https://school.programmers.co.kr/learn/courses/30/lessons/250126?language=java
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];
//기존 코드 : clean_storage[num_item] = Integer.toString(num[i]);
//clean_storage에는 storage에 있는 String이 들어간다.
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;
}
}
헷갈렸던 부분 : 초반에 int num_item = 0; 으로 설정해서 for문 조건문에서 오류날 거라 생각했다.
-> for문 조건문에 안맞으면 통과하고 아래에서 num_item += 1 하고 다시 for문을 돌게 된다.