[Programmers_CT] 16. 창고 정리

문정준's avatar
Feb 18, 2025
[Programmers_CT] 16. 창고 정리
 
💡
프로그래머스 코딩 테스트 16. 창고 정리

1. 문제 설명 - 디버깅

notion image

제한 사항

notion image

2. 입출력 예시

notion image
notion image

3. 코드 수정

class Solution { public String solution(String[] storage, int[] num) { int num_item = 0; String[] clean_storage = new String[storage.length]; int[] clean_num = new int[num.length]; for(int i=0; i<storage.length; i++){ int clean_idx = -1; for(int j=0; j<num_item; j++){ if(storage[i].equals(clean_storage[j])){ clean_idx = j; break; } } if(clean_idx == -1){ clean_storage[num_item] = storage[i].toString(); clean_num[num_item] = num[i]; num_item += 1; } else{ clean_num[clean_idx] += num[i]; } } // 아래 코드에는 틀린 부분이 없습니다. int num_max = -1; String answer = ""; for(int i=0; i<num_item; i++){ if(clean_num[i] > num_max){ num_max = clean_num[i]; answer = clean_storage[i]; } } return answer; } }
  • storage : 창고의 각 칸에 담겨있는 물품을 정리하지 않고 나열한 배열
  • num : 창고의 각 칸에 담겨있는 물품의 개수를 나열한 배열
  • clean_storage : 창고의 중복된 물품들을 제거한 물품의 종류의 배열
  • clean_num : 창고의 중복된 물품들의 개수를 합친 물품의 개수의 배열
 
코드 작동 구조
  1. 정리할 물품의 배열 초기화 (clean_storage, clean_num)
  1. clean_idx 초기화, 물품이 중복되었는지 비교
    1. 중복되었으면 clean_idx = 중복된 물품이 있는 인덱스
    2. 아니라면 (새 물품) 물품의 이름과 개수를 정리할 물품의 배열에 추가
        • 물품의 이름을 저장해야 함
        • Integer.toString(num[i]) → storage[i].toString()
 

4. 테스트 실행

notion image

5. 제출 후 채점

notion image
Share article

sxias