[프로그래머스][체육복]-Lv.1

호준·2022년 11월 16일
0

Algorithm

목록 보기
99/111
post-thumbnail

🎐 문제

문제링크

🎐 제한사항

🎐 접근방법

  1. 제한사항의 5번의 경우를 먼저 처리한다.
  2. 여벌있는 번호의 앞뒤 번호가 잃어버렸는지 확인한다.

🎐 코드

import java.util.*;
class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        int answer = 0;
        Arrays.sort(reserve);
        List<Integer> lostList = new ArrayList<>();
        for(int i : lost){
            lostList.add(i);
        }
        for(int i=0; i<reserve.length; i++){
            if(lostList.contains(reserve[i])){
                lostList.remove(Integer.valueOf(reserve[i]));
                reserve[i] = -1;
            }
        }
        // 1. 빌려줄 수 있는 애 체크
        for(int i=0; i<reserve.length; i++){
            if(reserve[i]==-1) continue;
            // 2. 양옆 확인 후 lost에 있으면 빌려주기
            if(lostList.contains(reserve[i]-1)){
                lostList.remove(Integer.valueOf(reserve[i]-1));
                continue;
            }
            if(lostList.contains(reserve[i]+1)){
                lostList.remove(Integer.valueOf(reserve[i]+1));    
                continue;
            }
        }
        return n - lostList.size();
    }
}
profile
도전하지 않는 사람은 실패도 성공도 없다

0개의 댓글