FrogRiverOne
Find the earliest time when a frog can jump to the other side of a river.
Task Score
100%
Correctness
100%
Performance
100%
아 원래 풀어 두었던 코드가 날아가서 재시도 ㅠㅠ.... 원래 풀이가 뭐였는지 전혀 기억이 나지 않아 새로운 마음으로 HashMap
클래스를 사용해서 풀었다.
X보다 작은 값만 중복되지않게 맵에 넣어주면 1, 2, 3, ..., X가 되는 순간 hashmap의 size도 X와 같게 되기 때문에 그 순간의 key값을 return하도록 했다.
xxxxxxxxxx
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int X, int[] A) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < A.length; i++) {
if (A[i] <= X && map.get(A[i]) == null)
map.put(A[i], 1);
if (map.size() == X) {
return i;
}
}
return -1;
}
}
'공부 > algorithm' 카테고리의 다른 글
[codility] PermCheck (0) | 2018.05.23 |
---|---|
[codility] MissingInteger (0) | 2018.05.23 |
[codility] TapeEquilibrium (0) | 2018.05.22 |
[codility] PermMissingElem (2) | 2018.05.22 |
[codility] FrogJmp (0) | 2018.05.22 |
댓글