Skip to content

2/4 베스트 앨범 #49

Open
Open
@skarltjr

Description

@skarltjr
package algo;


import java.util.*;

/**
 * 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범
 * 속한 노래가 많이 재생된 장르를 먼저 수록합니다.
 * 장르 내에서 많이 재생된 노래를 먼저 수록합니다.
 * 장르 내에서 재생 횟수가 같은 노래 중에서는 고유 번호가 낮은 노래를 먼저 수록합니다.
 * genres[i] 는 i 번쨰노래의 장르 plays[i]는 i번째 노래의 재생횟수
 *
 * 즉 장르별로 재생 총 재생횟수에 따라 장르 순서 정렬필요
 * 장르에 포함된 노래들 재생횟수에 따라 노래 정렬필요
 * 그럼 결과적으로 장르마다 가장 횟수 많은 2개씩 배열에 add /return
 * */
public class Solution {
    public int[] solution(String[] genres, int[] plays) {

        // 1. 2개의 해쉬맵   고유번호와 장르  / 고유번화 재생횟수
        HashMap<Integer, String> hashWithGenre = new HashMap<>();
        HashMap<Integer, Integer> hashWithCount = new HashMap<>();
        Set<String> genre = new HashSet<>();

        for (int i = 0; i < genres.length; i++) {
            hashWithGenre.put(i, genres[i]);
            hashWithCount.put(i, plays[i]);
            genre.add(genres[i]);
        }


        HashMap<String, Integer> genreWithCount = new HashMap<>();
        // 2. 장르별 총 재생횟수 구해주고
        for (String s : genre)
        {
            genreWithCount.put(s, 0);
        }

        for (int i = 0; i < genres.length; i++)
        {
            String str = hashWithGenre.get(i);
            Integer num = hashWithCount.get(i);
            genreWithCount.replace(str, genreWithCount.get(str)+num);
        }
        // 3. 장르별 총 재생횟수에 따라 장르정렬 내림차순으로
        ArrayList<String> orderedGenre = new ArrayList<>(genreWithCount.keySet());
        Collections.sort(orderedGenre, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return genreWithCount.get(o2)-genreWithCount.get(o1);
            }
        });
        genre = new HashSet<>();
        for (String s : orderedGenre) {
            genre.add(s);
        }
        //genre는 내림차순으로 정렬되어있다
        ArrayList<Integer> arr = new ArrayList<>();
        //todo 장르의 순서를 알았다 - > 장르별로 최대재생 노래 2곡을 골라내야한다
        for (String key : orderedGenre) {
            int first = 0;
            int firstIndex = 0;
            int second = 0;
            int secondIndex = 0;
            for (int i = 0; i < genres.length; i++) {
                if (hashWithGenre.get(i).equals(key)) {
                    if (hashWithCount.get(i) >= first) {
                        if (hashWithCount.get(i) == first) {
                            second = hashWithCount.get(i);
                            secondIndex = i;
                        } else {
                            second = first;
                            secondIndex = firstIndex;
                            first = hashWithCount.get(i);
                            firstIndex = i;
                        }
                    } else if (hashWithCount.get(i) < first && hashWithCount.get(i) > second) {
                        second = hashWithCount.get(i);
                        secondIndex = i;
                    }
                }
            }
            if (first != 0) {
                arr.add(firstIndex);
            }
            if (second != 0) {
                arr.add(secondIndex);
            }
        }

        int[] answer = new int[arr.size()];
        int index = 0;
        for (Integer i : arr) {
            answer[index] = i;
            index++;
        }
        return answer;
    }

}



Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions