728x90
건너지 못하는 경우일 때 return -1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public int solution(int X, int[] A) { // write your code in Java SE 8 boolean[] canJump = new boolean[X + 1]; int pre = 0; int index = 0; for (; index < A.length; index++) { canJump[A[index]] = true; if (A[index] == pre + 1) { pre++; while (pre < X) { if (canJump[pre + 1]) pre++; else break; } } if (pre == X) break; } if (pre == X) return index; else return -1; } | cs |
'알고리즘 풀이 > Codility' 카테고리의 다른 글
codility - Counting Elements (PermCheck) (0) | 2019.12.10 |
---|---|
codility - Counting Elements (MaxCounters) (0) | 2019.12.10 |
codility - Time Complexity (TapeEquilibrium) (0) | 2019.12.10 |
codility - Time Complexity (PermMissingElem) (0) | 2019.12.10 |
codility - Time Complexity (FrogJmp) (0) | 2019.12.10 |