Here is my answers to Cod1l1ty training lesson 04 - PART02.
PassingCars
GenomicRangeQueryfunction solution($A) {
$tot = 0;
foreach($A as $val) {
$tot += $val;
}
$pass = 0;
$curr = 0;
foreach($A as $val) {
if($val == 0) {
$pass += ($tot - $curr);
}
$curr += $val;
}
return ($pass > 1000000000 ? -1 : $pass);
}
function solution($S, $P, $Q) { $n = strlen($S); $s = []; for($i = 0; $i < $n; $i++) { switch($S[$i]) { case 'A': $s[] = 1; break; case 'C': $s[] = 2; break; case 'G': $s[] = 3; break; default: $s[] = 4; break; } } $n = count($P); $ret = []; for($i = 0; $i < $n; $i++) { $min = 0; for($j = $P[$i]; $j <= $Q[$i]; $j++) { if($min == 1) { break; } if($min > $s[$j] || $min == 0) { $min = $s[$j]; } } $ret[] = $min; } return $ret; }
Note: not 100% yet