-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP42579.java
86 lines (71 loc) · 2.57 KB
/
P42579.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package programmers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
class Song implements Comparable<Song> {
private int id;
private int playCount;
private String genre;
public Song(int id, int playCount, String genre) {
this.id = id;
this.playCount = playCount;
this.genre = genre;
}
public int getId() {
return id;
}
public int getPlayCount() {
return playCount;
}
public String getGenre() {
return genre;
}
public int compareTo(Song s) {
return this.playCount - s.getPlayCount();
}
}
class Chart {
private HashMap<String, ArrayList<Song>> songsByGenre;
private HashMap<String, Integer> playCountByGenre;
public Chart() {
songsByGenre = new HashMap<>();
playCountByGenre = new HashMap<>();
}
public void addSong(Song song) {
String genre = song.getGenre();
if (!songsByGenre.containsKey(genre)) {
songsByGenre.put(genre, new ArrayList<>());
playCountByGenre.put(genre, 0);
}
songsByGenre.get(genre).add(song);
playCountByGenre.put(genre, playCountByGenre.get(genre) + song.getPlayCount());
}
public ArrayList<String> getGenresByPlayCount() {
return playCountByGenre.keySet().stream()
.sorted((a, b) -> playCountByGenre.get(b) - playCountByGenre.get(a))
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
}
public ArrayList<Song> getSongsInGenreByPlayCount(String genre) {
ArrayList<Song> songs = songsByGenre.get(genre);
Collections.sort(songs, Collections.reverseOrder());
return songs;
}
}
public class P42579 {
public int[] solution(String[] genres, int[] plays) {
Chart chart = new Chart();
for (int i = 0; i < genres.length; i++)
chart.addSong(new Song(i, plays[i], genres[i]));
ArrayList<Integer> ans = new ArrayList<Integer>();
for (String genre : chart.getGenresByPlayCount()) {
ArrayList<Song> songs = chart.getSongsInGenreByPlayCount(genre);
for (int i = 0; i < Math.min(2, songs.size()); i++)
ans.add(songs.get(i).getId());
}
return ans.stream().mapToInt(i -> i).toArray();
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new P42579().solution(new String[] {"classic", "pop", "classic", "classic", "pop"}, new int[] {500, 600, 150, 800, 2500})));
}
}