class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        int answer = 0;
        int[] clothes = new int[n];
        for(int l : lost) {
            clothes[l-1]--;
        }
        for(int r : reserve) {
            clothes[r-1]++;
        }
        
        for(int i = 0; i<clothes.length;++i) {
            if(clothes[i] >0 && i > 0 && clothes[i-1] == -1) {
                clothes[i-1]++;
                clothes[i]--;
                System.out.println(i+ " "+ (i-1));
            }
            else if(clothes[i] > 0 && i+1 <clothes.length && clothes[i+1] == -1) {
                clothes[i+1]++;
                clothes[i]--;
                System.out.println(i+ " "+ (i+1));
            }
            /*
            if(clothes[i] > 0 && i+1 <clothes.length) {
                clothes[i+1]++;
                clothes[i]--;
            }*/
            
        }
        for(int i : clothes) {
            System.out.print(i);
            if(i >= 0)
                answer++;
        }
        return answer;
    }
}

처음 풀 때 어디서 꼬였었는데 넘겼다가 오늘 다시 풀어봤다.

clothes[]를 초기화하면 기본적으로 다 0이 들어가 있으므로 0을 옷을 1장 보유한 상태로 가정한다.

도둑맞은걸 빼고 여분을 더해준 뒤해 알고리즘이 시작된다.

왼쪽에서 오른쪽으로 진행될 예정이기 때문에 왼쪽을 우선적으로 빌려주고 왼쪽이 옷을 가지고 있을 경우, 오른쪽에 빌려주는 형태로 진행이 된다.

여분을 계속 우측으로 넘기면 좋지 않을까 싶었지만 옷은 없는 사람한테만 직접 빌려줘야 하나보다.

 

'문제풀이' 카테고리의 다른 글

프로그래머스(섬 연결하기)  (0) 2021.06.08
프로그래머스(조이스틱)  (0) 2021.06.08
프로그래머스(큰 수 만들기)  (0) 2021.06.07
프로그래머스(구명보트)  (0) 2021.06.07
프로그래머스(단속카메라)  (0) 2021.06.07

+ Recent posts