-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
103 lines (83 loc) · 3.13 KB
/
Solution.java
File metadata and controls
103 lines (83 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import java.util.*;
import java.util.stream.Collectors;
class Song {
String genre;
int playCnt;
int idx;
public Song(String genre, int playCnt, int idx) {
this.genre = genre;
this.playCnt = playCnt;
this.idx = idx;
}
}
class Genre {
ArrayList<Song> songs;
int totalCnt;
public Genre(){
this.songs = new ArrayList<Song>();
this.totalCnt = 0;
}
public void addSong(Song song, int cnt){
this.songs.add(song);
Collections.sort(this.songs, (s1, s2) -> {
return Integer.compare(s2.playCnt, s1.playCnt);
});
this.totalCnt += cnt;
}
public int getTotalCnt(){
return this.totalCnt;
}
}
class Solution {
public static void main(String[] arg){
String[] p1 = {"classic","pop","classic","classic","pop"};
int[] p2 = {500,600,150,800,2500};
int[] rlt = {};
rlt = solution(p1, p2);
for(int i : rlt) System.out.print(i + " ");
}
public static int[] solution(String[] genres, int[] plays) {
ArrayList<Integer> answer = new ArrayList<Integer>();
ArrayList<Song> songList = new ArrayList<Song>();
Map<String, Genre> genreList= new HashMap<String, Genre>();
for(int i=0; i<genres.length; i++){
songList.add(new Song(genres[i], plays[i], i));
}
for(Song song : songList){
if(genreList.containsKey(song.genre)) {
genreList.get(song.genre).addSong(song, song.playCnt);
}else{
genreList.put(song.genre, new Genre());
genreList.get(song.genre).addSong(song, song.playCnt);
}
}
// genreList를 totalCnt 순으로 내림차순 해야됨.
genreList = genreList.entrySet()
.stream()
.sorted((s1,s2) -> Integer.compare(s2.getValue().totalCnt, s1.getValue().totalCnt))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
Iterator<String> keys = genreList.keySet().iterator();
while(keys.hasNext()){
String name = keys.next();
System.out.println(name + ": " + Integer.toString(genreList.get(name).totalCnt));
if(genreList.get(name).songs.size() >= 2){
for(int i=0; i<2; i++){
System.out.println(genreList.get(name).songs.get(i).idx);
answer.add(genreList.get(name).songs.get(i).idx);
}
} else {
for(int i=0; i<1; i++){
System.out.println(genreList.get(name).songs.get(i).idx);
answer.add(genreList.get(name).songs.get(i).idx);
}
}
}
int[] ans = new int[answer.size()];
int cnt = 0;
for(int tmp : answer){
ans[cnt++] = tmp;
}
return ans;
}
}