CountDiv
function solution($a, $b, $k) { $i = ($a % $k == 0 ? $a : $a + ($k - ($a % $k))); $ret = 0; if ($i < $b) { $ret = (int)(($b - $i) / $k) + 1; }elseif($i == $a){ $ret = 1; } return $ret; }
PassingCars
function solution($A) { $n = count($A); $sum = []; $m = 0; for($i = 0; $i < $n; $i++) { if($A[$i] == 0) { $sum[] = 0; $m++; }elseif($m > 0){ $sum[$m - 1]++; } } $sumTot = 0; for($i = 0; $i < $m; $i++) { $sumTot += ($sum[$i] + ($i * $sum[$i])); if($sumTot > 1000000000) { return -1; } } return $sumTot; }
MinAvgTwoSlice
function solution($A) { $n = count($A); $idx = 0; $min = null; $sums = [$A[0]]; for($p = 0; $p < $n - 1; $p++) { for($q = $p + 1; $q < $n; $q++) { $sums[$q] = $sums[$q - 1] + $A[$q]; $avg = ($sums[$q] - $sums[$p] + $A[$p]) / ($q - $p + 1); if($avg < $min || $min === null) { $min = $avg; $idx = $p; } } } return $idx; }
GenomicRangeQuery
function solution($S, $P, $Q) { $ret = []; $m = count($P); for($i = 0; $i < $m; $i++) { $min = ''; for($j = $P[$i]; $j <= $Q[$i]; $j++) { if($min == '' || $min > $S[$j]) { $min = $S[$j]; } } switch($min) { case 'A': $ret[] = 1; break; case 'C': $ret[] = 2; break; case 'G': $ret[] = 3; break; default: $ret[] = 4; } } return $ret; }
Wah, untuk yang 2 terakhir susah nih. gak bisa dapet 100%.
ReplyDeletemungkin otak saya lagi mandek, kalo ntr dapet ilham bakal saya benerin.