import java.util.Arrays;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = n - lost.length;
Arrays.sort(lost);
Arrays.sort(reserve);
//여벌 체육복을 가져온 학생이 체육복을 도난당한 경우
for(int i=0; i<lost.length; i++){
for(int j=0; j<reserve.length; j++){
if(lost[i] == reserve[j]){
answer++;
lost[i] = -1;
reserve[j] = -1;
break;
}
}
}
//여벌 체육복이 있는 학생만 다른 학생에게 체육복을 빌려줄 수 있는 경우
for(int i=0; i<lost.length; i++){
for(int j=0; j<reserve.length; j++){
if(lost[i]-1 == reserve[j]
|| lost[i]+1 == reserve[j]){
answer++;
reserve[j] = -1;
break;
}
}
}
return answer;
}
}
해당 문제에서는 주의해야 할 제한 사항이 2가지 있다.
- 여벌 체육복이 있는 학생만 다른 학생 1명에게 체육복을 빌려줄 수 있습니다.
- 여벌 체육복을 가져온 학생이 체육복을 하나만 도난당했을 수 있습니다.
int answer = n - lost.length;
Arrays.sort(lost);
Arrays.sort(reserve);
바로 앞번호의 학생이나 바로 뒷번호의 학생에게만 체육복을 빌려줄 수 있다.