Coding Test/Codility
Codility - Perm Missing Elem 풀이 (java)
dev-tempcru
2022. 1. 5. 14:33
문제
https://app.codility.com/programmers/lessons/3-time_complexity/perm_missing_elem/
PermMissingElem coding task - Learn to Code - Codility
Find the missing element in a given permutation.
app.codility.com
- 배열에서 없는 숫자하나 찾아라~~
접근방법
- 주어진 배열안에 들어갈 숫자가 최대 10만 1 이므로
- value를 index로 하는 boolean 배열 하나 만들어서 exist 체크
풀이
public class PermMissingElem {
public static void main(String[] args) {
int[] A = {2, 3, 1, 5};
System.out.println(solution(A));
}
private static int solution(int[] A) {
// init
int result = 0;
boolean[] test = new boolean[A.length + 2];
//logic
for(int i : A) {
test[i] = true;
}
for(int i = 1; i < test.length; i++) {
if(!test[i]) {
result = i;
break;
}
}
return result;
}
}