-
Notifications
You must be signed in to change notification settings - Fork 6
/
JIHANEOL.java
72 lines (63 loc) · 1.65 KB
/
JIHANEOL.java
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
import java.util.*;
public class JIHANEOL {
public static void main(String[] args) {
String[] genres = new String[] { "classic", "pop", "classic", "classic", "pop" };
int[] plays = new int[] { 500, 600, 150, 800, 2500 };
Solution s1 = new Solution();
if (Arrays.equals(new int[] {4,1,3,0}, s1.solution(genres, plays))) {
System.out.println("통과");
} else {
System.out.println("땡");
}
}
}
class Album{
Map<String, Music> map = new HashMap();
}
class Music implements Comparable<Music>{
int total;
List<int[]> list = new ArrayList(); // idx,plays..
public Music(int total) {
this.total = total;
}
@Override
public int compareTo(Music o) {
return o.total- this.total ;
}
}
class Solution {
public int[] solution(String[] genres, int[] plays) {
List<Integer> answer = new ArrayList();
Album album = new Album();
Map<String, Music> map = album.map;
for(int i=0; i<genres.length; i++){
if(map.containsKey(genres[i])) {
Music m = map.get(genres[i]);
m.list.add(new int[] {i,plays[i]});
m.total+=plays[i];
}else {// 없다면
map.put(genres[i], new Music(plays[i]));
Music m = map.get(genres[i]);
m.list.add(new int[] {i,plays[i]});
}
}
// 장르 선택하기
List<Music> m = new ArrayList(map.values());
Collections.sort(m);
for(int i=0; i<m.size(); i++) {
Music mu = m.get(i);
List<int[]> l = mu.list;
l.sort((a,b)->{
return b[1]-a[1]==0?a[0]-b[0]:b[1]-a[1];
});
if(l.size()==1) {
answer.add(l.get(0)[0]);
}else {
answer.add(l.get(0)[0]);
answer.add(l.get(1)[0]);
}
}
System.out.println(answer);
return new int[] {0};
}
}