-
프로그래머스 - 체육복Algorithm 2023. 5. 6. 23:01
문제
풀이
이번 문제는 제법 시간이 꽤 걸렸었다. 풀고자 했던 의도는 먼저 reserve 배열의 요소중 lost 배열의 요소와 같은것이 있는지 즉, 여분의 체육복이 있으면서 도난당한 학생이 있는지 확인하고 해당 학생번호를 lost 배열과 reserve 배열 각각 제거시켜 주었다. 그 후 reserve 배열의 학생번호 앞 뒤 번호가 lost 배열에 있는지 체크하고 있다면 체육복을 가진 학생 수를 증가시켜준다.
하지만, 이론으로는 그럴듯 해 보였으나 계속 한 두 케이스에서 실패가 나서 왜 그런지 이유를 도저히 모르던 와중 lost 배열이나 reserve 배열의 학생 번호가 정렬되어 있지 않아서 발생하는 문제가 있었다.
5명의 학생이 있고, lost 배열에는 [2, 4], reserve 배열에는 [3, 1]이 입력값으로 주어진다면 이상적인 결과로는 1번학생이 2번에게 빌려주고, 3번학생이 4번학생에게 빌려주어서 5명 모두 체육복을 가지게 되어야하는데 코드상으로는 3번학생이 2번학생에게 빌려주어 1번학생이 빌려줄 수 없게 된 것이다.
따라서, 코드 가장 윗 부분에서 lost 배열과 reserve 배열을 정렬 시켜주는 작업을 추가해주었고, 정상 작동하였다.
아쉬운 점은 분명 배열이 정렬이 되어 있지 않다라는 정보가 문제에 없었다는건 정렬할 필요없이 해결할 수 있는 문제였다는 것인데 억지로 끼워맞춘 기분이들어 아쉬웠다.
'Algorithm' 카테고리의 다른 글
프로그래머스 - 과일 장수 (1) 2023.05.12 프로그래머스 - 신고 결과 받기 (1) 2023.05.12 프로그래머스 - [카카오 인턴] 키패드 누르기 (0) 2023.05.06 프로그래머스 - 두 개 뽑아서 더하기 (0) 2023.05.04 프로그래머스 - 덧칠하기 (0) 2023.05.03