문제 설명

점심시간에 도둑이 들어, 일부 학생이 체육복을 도난당했습니다. 다행히 여벌 체육복이 있는 학생이 이들에게 체육복을 빌려주려 합니다. 학생들의 번호는 체격 순으로 매겨져 있어, 바로 앞번호의 학생이나 바로 뒷번호의 학생에게만 체육복을 빌려줄 수 있습니다. 예를 들어, 4번 학생은 3번 학생이나 5번 학생에게만 체육복을 빌려줄 수 있습니다. 체육복이 없으면 수업을 들을 수 없기 때문에 체육복을 적절히 빌려 최대한 많은 학생이 체육수업을 들어야 합니다.

전체 학생의 수 n, 체육복을 도난당한 학생들의 번호가 담긴 배열 lost, 여벌의 체육복을 가져온 학생들의 번호가 담긴 배열 reserve가 매개변수로 주어질 때, 체육수업을 들을 수 있는 학생의 최댓값을 return 하도록 solution 함수를 작성해주세요.

제한사항

입출력 예

Untitled

입출력 예 설명

예제 #11번 학생이 2번 학생에게 체육복을 빌려주고, 3번 학생이나 5번 학생이 4번 학생에게 체육복을 빌려주면 학생 5명이 체육수업을 들을 수 있습니다.

예제 #23번 학생이 2번 학생이나 4번 학생에게 체육복을 빌려주면 학생 4명이 체육수업을 들을 수 있습니다.

시도1. 체육복을 빌리는 과정을 한번의 순회에 수행

package programmers.greedy;

//contains 사용?
public class Q1 {
    public static void main(String[] args) {
        class Solution {
            public int solution(int n, int[] lost, int[] reserve) {
                int[] array = new int[n];
                // 처음에 모두가 체육복을 가짐
                for (int i = 0; i < n; i++) {
                    array[i] = 1;
                }
                // lost 적용
                for (int v : lost) {
                    array[v - 1] = 0;
                }
                // reserve 적용
                for (int v : reserve) {
                    array[v - 1] = 2;
                }
                // 체육복 빌리기
                for (int i = 0; i < n; i++) {
                    if (array[i] == 0) {
                        if (i + 1 != n && array[i + 1] == 2) {
                            array[i]++;
                            array[i - 1]--;
                            continue;
                        }
                        if (i != 0 && array[i - 1] == 2) {
                            array[i]++;
                            array[i - 1]--;
                        }
                    }
                }
                int result = 0;
                for (int v : array) {
                    if (v != 0)
                        result++;
                }
                return result;
            }
        }
        //case 1
//        int n = 5;
//        int[] lost = {2, 4};
//        int[] reserve = {1, 3, 5};
        //case2
//        int n = 5;
//        int[] lost = {2, 4};
//        int[] reserve = {3};
        //case 3
//        int n = 3;
//        int[] lost = {3};
//        int[] reserve = {1};
        //case 4
        int n = 5;
        int[] lost = {1, 2, 3, 4, 5};
        int[] reserve = {1};

        Solution solution = new Solution();

        System.out.println(solution.solution(n, lost, reserve));
    }
}

→ 일부 테스트 케이스에서 실패함

시도2. 빌리는 과정 분리

package programmers.greedy;

import java.util.ArrayList;

//contains 사용?
public class Q1 {
    public static void main(String[] args) {
        class Solution {
            public int solution(int n, int[] lost, int[] reserve) {
                ArrayList<Integer> possible = new ArrayList<>();
                ArrayList<Integer> lostList = new ArrayList<>();
                for (int x : lost)
                    lostList.add(x);
                ArrayList<Integer> reserveList = new ArrayList<>();
                for (int x : reserve)
                    reserveList.add(x);
                for (int i = 0; i < n; i++) {
                    int count = 1;
                    if (lostList.contains(i + 1))
                        count--;
                    if (reserveList.contains(i + 1))
                        count++;
                    possible.add(count);
                }
                // 뒤 번호에게 빌리기
                for (int i = 0; i < possible.size() - 1; i++) {
                    if (possible.get(i) == 0 && possible.get(i + 1) == 2) {
                        possible.set(i, 1);
                        possible.set(i + 1, 1);
                    }
                }
                // 앞 번호에게 빌리기
                for (int i = 1; i < possible.size(); i++) {
                    if (possible.get(i) == 0 && possible.get(i - 1) == 2) {
                        possible.set(i, 1);
                        possible.set(i - 1, 1);
                    }
                }
                int count = 0;
                for (int val : possible) {
                    if (val != 0)
                        count++;
                }
                return count;
            }
        }
        Solution solution = new Solution();

        //case 1
        int n = 5;
        int[] lost = {2, 4};
        int[] reserve = {1, 3, 5};
        System.out.println(solution.solution(n, lost, reserve));
        //case2
        n = 5;
        lost = new int[]{2, 4};
        reserve = new int[]{3};
        System.out.println(solution.solution(n, lost, reserve));
        //case 3
        n = 3;
        lost = new int[]{3};
        reserve = new int[]{1};
        System.out.println(solution.solution(n, lost, reserve));
        //case 4
        n = 5;
        lost = new int[]{1, 2, 3, 4, 5};
        reserve = new int[]{1};
        System.out.println(solution.solution(n, lost, reserve));
				//case 5
        n = 5;
        lost = new int[]{3, 5};
        reserve = new int[]{2, 4};
        System.out.println(solution.solution(n, lost, reserve));
    }
}

→ case 5번을 통과하지 못함 → 순회하는 방향으로 앞 순서에 있는 사람에게 먼저 빌려야 Greedy문제에 만족하한다.