티스토리 뷰
https://app.codility.com/demo/results/training4C7QF3-WV6/
미디엄 레벨문제는 처음 ㅇ_ㅇ
이런 점수 또한 처음 ㅇ0ㅇ
댐의 최대수위 구하기 알듯.. 말듯...
function solution($A) { $result = 0; if (empty($A)) return $result; $N = sizeof($A); if ($N < 3 || $N > 100000) return $result; $max = 0; $tmp_max = 0; $tmp = array();$dep = array(); $max_dep = 0; for($i = 0 ; $i < sizeof($A) ; $i++) { if ($max < $A[$i]) { $max = $A[$i]; $tmp_max = 0; if(!empty($tmp)) { $dep = array_merge($dep, $tmp); $tmp = array(); } } else { if ($i != 0 && $i != $N-1) { $tmp[] = $max - $A[$i]; if ($A[$i] > $A[$i-1]) $tmp_max = $A[$i]; } } } if(!empty($tmp)) { if ($tmp_max > 0) { for ($i = 0 ; $i < sizeof($tmp) ; $i++) { $tmp[$i] -= $max-$tmp_max; } $dep = array_merge($dep, $tmp); } $tmp = array(); } foreach($dep as $tmp_dep) { $result = $result < $tmp_dep ? $tmp_dep : $result; } return $result; }
https://app.codility.com/demo/results/training8BTZY6-RW6/
원소값 범위 조건을 추가하고 마지막 값까지 확인할 수 있도록 수정하니 점수는 조금 올랐다.
그래도 갈 길이 멀다규... 다양한 테스트 케이스를 생각하는 능력?도 키워야 하는게 codility 문제들이다.
function solution($A) { $result = 0; if (empty($A)) return $result; $N = sizeof($A); if ($N < 3 || $N > 100000) return $result; $max = 0; $tmp_max = 0; $tmp = array();$dep = array(); $max_dep = 0; for($i = 0 ; $i < sizeof($A) ; $i++) { if ($A[$i] < 1 || $A[$i] > 100000000) return $result; if ($max < $A[$i]) { $max = $A[$i]; $tmp_max = 0; if(!empty($tmp)) { $dep = array_merge($dep, $tmp); $tmp = array(); } } else { if ($i != 0) { $tmp[] = $max - $A[$i]; if ($A[$i] > $A[$i-1]) $tmp_max = $A[$i]; } } } if(!empty($tmp)) { if ($tmp_max > 0) { for ($i = 0 ; $i < sizeof($tmp) ; $i++) { $tmp[$i] -= $max-$tmp_max; } $dep = array_merge($dep, $tmp); } $tmp = array(); } foreach($dep as $tmp_dep) { $result = $result < $tmp_dep ? $tmp_dep : $result; } return $result; }
'알고리즘' 카테고리의 다른 글
알고리즘/ 약수구하기 _PHP (0) | 2018.05.25 |
---|---|
알고리즘/ 빠진 양의 정수 찾기 _PHP (0) | 2018.05.22 |
알고리즘/ 가장 긴 패스워드 찾기 _PHP (0) | 2018.05.22 |
알고리즘/ 가장 짧은 거리 구하기 _PHP (0) | 2018.05.19 |
알고리즘/ 빠진 숫자 찾기 _PHP (0) | 2018.05.19 |