알고리즘 풀이/프로그래머스

[level1] 프로그래머스 - 체육복(JAVA)

데롱디롱 2021. 8. 27. 01:49
728x90

- clothes[] : 체육복이 없으면 -1, 있으면 0, 여벌로 있으면 1

- 체육복이 없는 아이 중에, 앞 사람한테 먼저 빌려보고 안 되면 뒤 아이에게 빌리도록 구현하였다.

 

 

class Solution {
    public int solution(int n, int[] lost, int[] reserve) {
        int answer = 0;

		int clothes[] = new int[n + 1];
		for (int i = 0; i < lost.length; i++)
			clothes[lost[i]] = -1;

		for (int i = 0; i < reserve.length; i++) {
			if (clothes[reserve[i]] == -1)
				clothes[reserve[i]] = 0;
			else
				clothes[reserve[i]] = 1;
		}

		for (int i = 0; i < clothes.length; i++) {
			if (clothes[i] == -1) {
				if (i - 1 >= 1 && clothes[i - 1] == 1) {
					clothes[i] = clothes[i - 1] = 0;
					continue;
				} else if (i + 1 < clothes.length && clothes[i + 1] == 1) {
					clothes[i] = clothes[i + 1] = 0;
					continue;
				}
				answer--;
			}
		}

		return answer + n;
    }
}