프로그래머스 [체육복]
문제보기

소스코드
import java.util.Arrays;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
Arrays.sort(lost);
Arrays.sort(reserve);
int lostLength = lost.length;
int borrowCnt = 0;
// 도둑 맞은 학생 중 본인 여벌은 본인이 챙기기
int jIdx = 0;
for (int i = 0; i < lostLength; i++) {
int lostNo = lost[i];
for (int j = jIdx; j < reserve.length; j++) {
if (lostNo == reserve[j]) {
borrowCnt++;
reserve[j] = -1;
lost[i] = -1;
jIdx++;
break;
}
}
}
// 도둑 맞은 학생 중 앞 뒤 학생에게 여벌 빌리기
jIdx = 0;
for (int i = 0; i < lostLength; i++) {
int lostNo = lost[i];
if (lostNo < 0) continue;
for (int j = jIdx; j < reserve.length; j++) {
int reserveNo = reserve[j];
if (reserveNo == lostNo - 1 || reserveNo == lostNo + 1) {
borrowCnt++;
reserve[j] = -1;
jIdx++;
break;
}
}
}
return n - lostLength + borrowCnt;
}
}