You are viewing a single comment's thread. Return to all comments →
Simple approach in php:
function minimumBribes($q) { $tooChaotic = false; $counter = 0; $sorted_array = $q; sort($sorted_array); while(($sorted_array != $q) && !$tooChaotic ){ for($i = 0; $i < count($q); $i++){ $how_far = abs( $q[$i] - ($i+1) ) ; if($how_far > 2){ $tooChaotic = true; break; } if( $i < count($q)-1){ if($q[$i] > $q[$i+1] ){ $current = $q[$i]; $next = $q[$i+1]; $q[$i] = $next; $q[$i+1] = $current; $counter++; } } } } if($tooChaotic){ echo "Too chaotic \n"; }else{ echo $counter . "\n"; } }
Seems like cookies are disabled on this browser, please enable them to open this website
New Year Chaos
You are viewing a single comment's thread. Return to all comments →
Simple approach in php: