728x90

import java.util.*;
class Solution {
    public int[] solution(String[] genres, int[] plays) {
        int[] answer = {};
        HashMap<String, Integer> genresMax = new HashMap<String, Integer>();
        HashMap<String, Integer[]> genTop2 = new HashMap<String, Integer[]>();
        for(int i = 0; i < plays.length ; i++){
            genresMax.put(genres[i],genresMax.getOrDefault(genres[i],0) + plays[i]);
        }
        // 장르 최대값
        int size = genresMax.size();
        String[] ge = new String[size];
        for(int i = 0; i < size; i++){
            int max = 0;
            String maxName = null;
            for(String s: genresMax.keySet()){
                if(genresMax.get(s) > max){
                    max = genresMax.get(s);
                    maxName = s;
                }
            }
            ge[i] = maxName;
            genresMax.remove(maxName);
        }
        // 값 담을 리스트
        List<Integer> list = new ArrayList<>();
        for(String s: ge){
            int rank1 = -1;
            int index1 = 0;
            int rank2 = -1;
            int index2 = 0;
            for(int i = 0; i < plays.length; i++){
                if(genres[i].equals(s)){
                    if(plays[i] > rank1){
                        rank2 = rank1;
                        index2 = index1;
                        rank1 = plays[i];
                        index1 = i;
                    }else if(plays[i] > rank2){
                        rank2 = plays[i];
                        index2 = i;
                    }
                }
            }
            list.add(index1);
            if(rank2 != -1){
                list.add(index2);
            }
        }
        answer = new int[list.size()];
        for(int i = 0; i < list.size(); i++){
            answer[i] = list.get(i);
        }
        return answer;
    }
}
728x90

+ Recent posts