코딩테스트연습/프로그래머스

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

:)jun 2021. 11. 28. 14:59

https://programmers.co.kr/learn/courses/30/lessons/42862

 

코딩테스트 연습 - 체육복

점심시간에 도둑이 들어, 일부 학생이 체육복을 도난당했습니다. 다행히 여벌 체육복이 있는 학생이 이들에게 체육복을 빌려주려 합니다. 학생들의 번호는 체격 순으로 매겨져 있어, 바로 앞번

programmers.co.kr

 

//테스트코드
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class GymSuitTest {

    GymSuit q;

    @BeforeEach
    void setUp() {
        q = new GymSuit();
    }

    @Test
    void test1() {
        assertEquals(5, q.solution(5, new int[]{2, 4}, new int[]{1, 3, 5}));
    }

    @Test
    void test2() {
        assertEquals(4, q.solution(5, new int[]{2, 4}, new int[]{3}));
    }

    @Test
    void test3() {
        assertEquals(2, q.solution(3, new int[]{3}, new int[]{1}));
    }
}

//프로덕션코드
import java.util.ArrayList;
import java.util.List;

public class GymSuit {
    public int solution(int n, int[] lost, int[] reserve) {
        List<Integer> lostList = new ArrayList<>();
        List<Integer> reserveList = new ArrayList<>();


        int numberOfStudentsWhohaveGymSuit = n;

        for (int i = 0; i < lost.length; i++) {
            lostList.add(lost[i]);
        }
        for (int i = 0; i < reserve.length; i++) {
            reserveList.add(reserve[i]);
        }

        for (int i = 0; i < lostList.size(); i++) {
            for (int j = 0; j < reserveList.size(); j++) {
                if (lostList.get(i) == reserveList.get(j)) {
                    lostList.remove(i);
                    reserveList.remove(j);
                    // 객체로 지우는 것도 있고 인덱스로 지우는 것도 있는데
                    // 이렇게 Integer 객체일때는...? 숫자 객체라는 것이 없나..?
                    // 객체 공부 더 해보자.
                }
            }
        }

        numberOfStudentsWhohaveGymSuit -= lostList.size();

        for (int i = 0; i < reserveList.size(); i++) {
            for (int j = 0; j < lostList.size(); j++){
                if(reserveList.get(i)-1 == lostList.get(j)){
                    reserveList.remove(i);
                    lostList.remove(j);
                    numberOfStudentsWhohaveGymSuit++;
                    i--;
                    break;
                }
                if(reserveList.get(i)+1 == lostList.get(j)){
                    reserveList.remove(i);
                    lostList.remove(j);
                    numberOfStudentsWhohaveGymSuit++;
                    i--;
                    break;
                }
            }
        }

        return numberOfStudentsWhohaveGymSuit;
    }
}
// 몇개 케이스 실패! 런타임에러도 하나 있고...
// 일단 도난당한 사람이랑 여분 있는 사람이 같을 때 지워주는 과정에서 i-- 추가해야 빠짐없이 확인할 수 있겠네

import java.util.ArrayList;
import java.util.List;

public class GymSuit {
    public int solution(int n, int[] lost, int[] reserve) {
        List<Integer> lostList = new ArrayList<>();
        List<Integer> reserveList = new ArrayList<>();


        int numberOfStudentsWhohaveGymSuit = n;

        for (int i = 0; i < lost.length; i++) {
            lostList.add(lost[i]);
        }

        for (int i = 0; i < reserve.length; i++) {
            reserveList.add(reserve[i]);
        }

        for (int i = 0; i < lostList.size(); i++) {
            for (int j = 0; j < reserveList.size(); j++) {
                if (lostList.get(i) == reserveList.get(j)) {
                    lostList.remove(i);
                    reserveList.remove(j);
                    i--;
                    break;
                }
            }
        }

        numberOfStudentsWhohaveGymSuit -= lostList.size();

        for (int i = 0; i < reserveList.size(); i++) {
            for (int j = 0; j < lostList.size(); j++){
                if(reserveList.get(i)-1 == lostList.get(j)){
                    reserveList.remove(i);
                    lostList.remove(j);
                    numberOfStudentsWhohaveGymSuit++;
                    i--;
                    break;
                }
                if(reserveList.get(i)+1 == lostList.get(j)){
                    reserveList.remove(i);
                    lostList.remove(j);
                    numberOfStudentsWhohaveGymSuit++;
                    i--;
                    break;
                }
            }
        }

        return numberOfStudentsWhohaveGymSuit;
    }
}
// 런타임 오류 1개는 해결했는데 아직도 2개 실패!! 혹시 배열이 번호 순서대로 안되어있나..?
// 정렬하고 시작해보자.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GymSuit {
    public int solution(int n, int[] lost, int[] reserve) {

        Arrays.sort(lost);
        Arrays.sort(reserve);

        List<Integer> lostList = new ArrayList<>();
        List<Integer> reserveList = new ArrayList<>();


        int numberOfStudentsWhohaveGymSuit = n;

        for (int i = 0; i < lost.length; i++) {
            lostList.add(lost[i]);
        }

        for (int i = 0; i < reserve.length; i++) {
            reserveList.add(reserve[i]);
        }

        for (int i = 0; i < lostList.size(); i++) {
            for (int j = 0; j < reserveList.size(); j++) {
                if (lostList.get(i) == reserveList.get(j)) {
                    lostList.remove(i);
                    reserveList.remove(j);
                    i--;
                    break;
                }
            }
        }

        numberOfStudentsWhohaveGymSuit -= lostList.size();

        for (int i = 0; i < reserveList.size(); i++) {
            for (int j = 0; j < lostList.size(); j++){
                if(reserveList.get(i)-1 == lostList.get(j)){
                    reserveList.remove(i);
                    lostList.remove(j);
                    numberOfStudentsWhohaveGymSuit++;
                    i--;
                    break;
                }
                if(reserveList.get(i)+1 == lostList.get(j)){
                    reserveList.remove(i);
                    lostList.remove(j);
                    numberOfStudentsWhohaveGymSuit++;
                    i--;
                    break;
                }
            }
        }

        return numberOfStudentsWhohaveGymSuit;
    }
}
// 성공..! 진짜 너무하다! 테스트 케이스는 다 정렬하고 줘놓고선...
// 지레 짐작한 내 잘못이지.. 앞으로 더 꼼꼼하게 코딩해야겠다
// 리팩토링하고 마무리하자.

   Arrays.sort(lost);
        Arrays.sort(reserve);

        List<Integer> lostList = new ArrayList<>();
        List<Integer> reserveList = new ArrayList<>();

        int numberOfStudentsWhohaveGymSuit = n;

        setLostList(lost, lostList);
        setReserveList(reserve, reserveList);

        checkIfLostListandReserveListHaveSameStudent(lostList, reserveList);

        numberOfStudentsWhohaveGymSuit -= lostList.size();
        return getNumberOfStudentsWhohaveGymSuit(lostList, reserveList, numberOfStudentsWhohaveGymSuit);
    }

    private int getNumberOfStudentsWhohaveGymSuit(List<Integer> lostList, List<Integer> reserveList, int numberOfStudentsWhohaveGymSuit) {
        for (int i = 0; i < reserveList.size(); i++) {
            for (int j = 0; j < lostList.size(); j++){
                if(reserveList.get(i)-1 == lostList.get(j)){
                    reserveList.remove(i);
                    lostList.remove(j);
                    numberOfStudentsWhohaveGymSuit++;
                    i--;
                    break;
                }
                if(reserveList.get(i)+1 == lostList.get(j)){
                    reserveList.remove(i);
                    lostList.remove(j);
                    numberOfStudentsWhohaveGymSuit++;
                    i--;
                    break;
                }
            }
        }
        return numberOfStudentsWhohaveGymSuit;
    }

    private void checkIfLostListandReserveListHaveSameStudent(List<Integer> lostList, List<Integer> reserveList) {
        for (int i = 0; i < lostList.size(); i++) {
            for (int j = 0; j < reserveList.size(); j++) {
                if (lostList.get(i) == reserveList.get(j)) {
                    lostList.remove(i);
                    reserveList.remove(j);
                    i--;
                    break;
                }
            }
        }
    }

    private void setReserveList(int[] reserve, List<Integer> reserveList) {
        for (int i = 0; i < reserve.length; i++) {
            reserveList.add(reserve[i]);
        }
    }

    private void setLostList(int[] lost, List<Integer> lostList) {
        for (int i = 0; i < lost.length; i++) {
            lostList.add(lost[i]);
        }
    }
}

// 성공!