import java.util.Arrays;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
Arrays.sort(lost);
Arrays.sort(reserve);
int count = 0;
boolean flag = false;
boolean[] isUsed = new boolean[reserve.length];
//여벌있는 사람이 도난 당한 경우 선처리
for(int i=0; i<lost.length; i++) {
for(int j=0; j<reserve.length; j++) {
if(lost[i]==reserve[j]) {
lost[i] = reserve[j] = 0;
count++;
}//if end
}//for end
}//for end
for(int i=0; i<lost.length; i++) {
if(lost[i]==0) continue;
//flag 초기화
flag = false;
for(int j=0; j<reserve.length; j++) {
if(reserve[j]==0) continue;
if(!isUsed[j]) {
switch(lost[i]-reserve[j]) {
case 1: count++;
flag = true;
isUsed[j] = true;
break;
case -1: count++;
flag = true;
isUsed[j] = true;
break;
}//switch case end
}//if end
if(flag) break;
}//for end
}//for end
return n-lost.length+count;
}
}
다른 사람 풀이
int[] people = new int[n];
for (int l : lost)
people[l-1]--;
for (int r : reserve)
people[r-1]++;
여벌 있는 사람이 도난 당한 경우 선처리를 이중for문이 아닌 번호-1이 index임을 이용해서 처리!
people 배열 이용하면 아래 과정도 이중for문 없이 처리 가능!
'자료구조 & 알고리즘 > 문제풀이' 카테고리의 다른 글
구명보트 - 프로그래머스 (Java, level 2) (0) | 2022.02.18 |
---|---|
큰 수 만들기 - 프로그래머스 (Java, Level 2) (0) | 2022.02.18 |
카펫 - 프로그래머스 (Java, Level 2) (0) | 2022.02.10 |
가장 큰 수 - 프로그래머스 (Java, Level 2) (0) | 2022.02.09 |
K번째 수 - 프로그래머스 (Java, Level 1) (0) | 2022.02.09 |
댓글