728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42579
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
import java.util.*;
class Solution {
public Map<String,Integer> sumMap = new HashMap<>();
public Map<String,HashMap<Integer,Integer>> info = new HashMap<>();
public List<Integer> solution(String[] genres, int[] plays) {
for(int i=0;i<genres.length;i++){
String tempGen = genres[i];
// 입력값 정리
if(sumMap.containsKey(tempGen)){
int count = sumMap.get(tempGen) + plays[i];
sumMap.remove(tempGen);
sumMap.put(tempGen,count);
info.get(tempGen).put(i,plays[i]);
}else{
sumMap.put(tempGen,plays[i]);
HashMap<Integer,Integer> tempMap = new HashMap<>();
tempMap.put(i,plays[i]);
info.put(tempGen,tempMap);
}
}
List<String> orderedGen = getGenRank();
List<Integer> answer = getBestAl(orderedGen);
return answer;
}
public List<String> getGenRank(){
int max=-999;
List<String> keySet = new ArrayList(sumMap.keySet());
Collections.sort(keySet, (s1, s2) -> sumMap.get(s2) - (sumMap.get(s1)));
return keySet;
}
public List<Integer> getBestAl(List<String> orderedGen){
ArrayList<Integer> bestAl = new ArrayList<>();
for(String gen : orderedGen){
HashMap<Integer,Integer> map = info.get(gen);
List<Integer> musicLsit = new ArrayList(map.keySet());
Collections.sort(musicLsit,(s1,s2)->map.get(s2) - map.get(s1));
if(musicLsit.size()==1) {
bestAl.add(musicLsit.get(0));
continue;
}else{
bestAl.add(musicLsit.get(0));
bestAl.add(musicLsit.get(1));
}
}
return bestAl;
}
}
HashMap<장르, 총합>
HashMap<장르, HashMap<고유번호, 재생횟수>>
Map의 value값 기준 List 정렬
Collections.sort(keySet, (s1, s2) -> sumMap.get(s2) - (sumMap.get(s1)));
- List keySet에 있는 요수 s1, s2를
- " sumMap.get(s2)-sumMap.get(s1) "
- Map의 value값을 내림차순 정렬 기준으로 key값을 정렬
=> 가장 많이 재생 된 장르를 추출
Collections.sort(musicLsit,(s1,s2)->map.get(s2) - map.get(s1));
=> 같은 장르에 있는 노래를 재생 횟수를 기준으로 정렬
728x90
'🐣 알고리즘 삐약 > ✏️ 냅다 덤벼보는 문제풀이' 카테고리의 다른 글
[프로그래머스] 기능개발 | 스택/큐 | lv.2 | JAVA (0) | 2024.10.31 |
---|---|
[프로그래머스] 같은 숫자는 싫어 | 스택/큐 | lv.1 | JAVA (0) | 2024.10.31 |
[프로그래머스] 의상 | 해시 | lv.2 | JAVA (0) | 2024.10.31 |
[프로그래머스] 전화번호 목록 | 해시 | lv.2 | JAVA (0) | 2024.10.31 |
[프로그래머스] 완주하지 못한 선수 | 해시 | lv.1 | JAVA (0) | 2024.10.31 |