슈콩

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

Algorithms/Programmers

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

shukong 2025. 10. 9. 22:23

 

[문제]

코딩테스트 연습 - 체육복 | 프로그래머스 스쿨

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

 

[소스 코드]

import java.util.*;

class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        HashSet<Integer> lost_hs = new HashSet<>();
        HashSet<Integer> reserve_hs = new HashSet<>();
        for(int i : lost) lost_hs.add(i);
        for(int i : reserve) {
            if(lost_hs.contains(i)){
                lost_hs.remove(i);
            }
            else reserve_hs.add(i);
        }
        for(int i : reserve_hs){
            if(lost_hs.contains(i-1)){
                lost_hs.remove(i-1);
            }
            else if(lost_hs.contains(i+1)){
                lost_hs.remove(i+1);
            }
        }
        int answer = n - lost_hs.size();
        return answer;
    }
}