반응형

프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
CODE
import java.util.*;
class Solution {
public int solution(int n, int[] lost, int[] reserve) {
int answer = n - lost.length;
List<Integer> list = new ArrayList<>();
for(int l : reserve) {
list.add(l);
}
Arrays.sort(lost);
Collections.sort(list);
for(int i = 0; i < lost.length; i++) {
for(int j = 0; j < list.size(); j++) {
if(lost[i] == list.get(j)) {
list.remove(j);
lost[i] = -1;
answer++;
}
}
}
for(int i = 0; i < lost.length; i++) {
for(int j = 0; j < list.size(); j++) {
if(lost[i] - 1 == list.get(j) || lost[i] + 1 == list.get(j)) {
list.remove(j);
answer++;
break;
}
}
}
return answer;
}
}
이 문제의 핵심 예외사항은 아래와 같다.
1. 정렬
2. lost와 reserve의 중복 요소
이 두개이다. 나는 여분 체육복을 가진 사람이 체육복을 빌려주면 reserve(list) 원소를 제거해 나가는 식으로 짰다.
실행 결과

반응형