언어 바꾸기

박진은·2023년 5월 11일
0

코테

목록 보기
36/44
import java.util.*;

class Solution {
    public static class Song implements Comparable<Song> {
        int index;
        String genres;
        int plays;

        Song(int index, String genres, int plays) {
            this.index = index;
            this.genres = genres;
            this.plays = plays;
        }

        public String toString() {
            return index + " " + genres + " " + plays;
        }

        public int getIndex() {
            return this.index;
        }

        @Override
        public int compareTo(Song other) {
            if (this.plays > other.plays) {
                return -1;
            } else if (this.plays == other.plays) {
                return Integer.compare(this.index, other.index);
            } else {
                return 1;
            }
        }

        public int getPlays() {
            return plays;
        }
    }

    public static int[] solution(String[] genres, int[] plays) {
        Map<String, ArrayList<Song>> genreMap = new HashMap<>();
        for (int i = 0; i < genres.length; i++) {
            String genre = genres[i];
            Song song = new Song(i, genre, plays[i]);
            genreMap.putIfAbsent(genre, new ArrayList<>());
            genreMap.get(genre).add(song);
        }

        ArrayList<String> genreList = new ArrayList<>(genreMap.keySet());
        Collections.sort(genreList, (g1, g2) -> {
            int totalPlays1 = genreMap.get(g1).stream().mapToInt(Song::getPlays).sum();
            int totalPlays2 = genreMap.get(g2).stream().mapToInt(Song::getPlays).sum();
            return Integer.compare(totalPlays2, totalPlays1);
        });

        ArrayList<Integer> answerList = new ArrayList<>();
        for (String genre : genreList) {
            ArrayList<Song> songs = genreMap.get(genre);
            Collections.sort(songs);

            int count = 0;
            for (Song song : songs) {
                answerList.add(song.getIndex());
                count++;
                if (count >= 2) {
                    break;
                }
            }
        }

        int[] answer = new int[answerList.size()];
        for (int i = 0; i < answerList.size(); i++) {
            answer[i] = answerList.get(i);
        }

        return answer;
    }


}

언어를 파이썬에서 자바로 바꾸려고 하는데 겁나 후회된다. 하자만 간다.

 ArrayList<String> genreList = new ArrayList<>(genreMap.keySet());
        Collections.sort(genreList, (g1, g2) -> {
            int totalPlays1 = genreMap.get(g1).stream().mapToInt(Song::getPlays).sum();
            int totalPlays2 = genreMap.get(g2).stream().mapToInt(Song::getPlays).sum();
            return Integer.compare(totalPlays2, totalPlays1);
        });

이 부분에서 새로운 어레이 리스트를 생성하는데 위에서 색출한 장르의 집합으로 선언했다. 리얼...
그리고 해당 리스트를 정렬하는데 장르 map 의 각각의 플레이 수의 합으로 정렬했다.

profile
코딩

0개의 댓글