티스토리 뷰
728x90
https://programmers.co.kr/learn/courses/30/lessons/42579
[ 나의 풀이 ]
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Arrays;
class Solution {
public int[] solution(String[] genres, int[] plays) {
Map<String, Map<Integer, Integer>> musics = new HashMap<>();
Map<String, Integer> totals = new HashMap<>();
Map<Integer, Integer> playMap = null;
String genre = "";
for(int i = 0; i < genres.length; i++){
genre = genres[i];
totals.put(genre, totals.getOrDefault(genre,0) + plays[i]); //장르별 플레이 수 총 합 저장
playMap = musics.getOrDefault(genre, new HashMap<>()); //장르별 인덱스 리스트 저장
playMap.put(i, plays[i]);
musics.put(genre, playMap);
}
List<Map.Entry<String, Integer>> entries = new LinkedList<>(totals.entrySet()); //장르별 플레이 수 정렬
Collections.sort(entries, (o1, o2) -> o2.getValue().compareTo(o1.getValue()));
Map<Integer, Integer> musicMap = null;
List<Integer> result = new ArrayList<>();
for(Map.Entry<String, Integer> entry : entries) {
musicMap = musics.get(entry.getKey());
List<Map.Entry<Integer, Integer>> musicEntries = new LinkedList<>(musicMap.entrySet());
Collections.sort(musicEntries, ((o1, o2) -> o2.getValue().compareTo(o1.getValue())));
if(musicEntries.size() > 2) {
musicEntries = musicEntries.subList(0,2);
}
for(Map.Entry<Integer, Integer> musicEntry : musicEntries) {
result.add(musicEntry.getKey());
}
}
int[] answer = result.stream().
mapToInt(i -> i).toArray();
return answer;
}
}
[ Stream을 이용한 풀이 ]
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class Solution {
public int[] solution(String[] genres, int[] plays) {
return IntStream.range(0, genres.length) // genre 길이만큼 stream
.mapToObj(i -> new Music(genres[i], plays[i], i)) // Music object map put
.collect(Collectors.groupingBy(Music::getGenre)) // genre로 grouping
.entrySet().stream() // entryset stream
.sorted((a, b) -> sum(b.getValue()) - sum(a.getValue())) // sum기준 sorting
.flatMap(x -> x.getValue().stream().sorted().limit(2)) // 각 value 2개 까지 sorting
.mapToInt(x -> x.id).toArray(); // id를 array로 mapping
}
public class Music implements Comparable<Music> {
private int played;
private int id;
private String genre;
public Music(String genre, int played, int id) {
this.played = played;
this.id = id;
this.genre = genre;
}
public String getGenre() {
return genre;
}
@Override
public int compareTo(Music o) {
if(this.played == o.played) return this.id - o.id;
return o.played - this.played;
}
@Override
public String toString() {
return "Music{" +
"played=" + played +
", id=" + id +
", genre='" + genre + '\'' +
'}';
}
}
private int sum(List<Music> value) {
int answer = 0;
for(Music music : value) {
answer += music.played;
}
return answer;
}
}
728x90
'코딩테스트' 카테고리의 다른 글
[프로그래머스] 프린터 (0) | 2022.04.27 |
---|---|
[프로그래머스] 기능개발 (0) | 2022.04.19 |
[프로그래머스] 위장 (0) | 2022.04.01 |
[LeetCode] Valid Palindrome (0) | 2022.03.31 |
[LeetCode] Group Anagrams (0) | 2022.03.31 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크