Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Spring
- 스프링
- 프로그래머스
- 2020 KAKAO BLIND
- 포두부 보쌈
- 문자열 압축
- 동적 프로그래밍
- BFS
- 백준
- 2589
- 투어
- 호유동
- Servlet
- HTTP API
- 서블릿
- 스프링 MVC
- 2839
- 완도산회
- dp
- 설탕 배달
- mvc
- 맛집 투어
- 알고리즘
- 맛집
- 고모네 콩탕
- 다이나믹 프로그래밍
- 2638
- 1로 만들기
- 쓰레드 풀
- 양꼬치
Archives
- Today
- Total
프로그래밍 공방
[프로그래머스] 베스트앨범 본문
문제
programmers.co.kr/learn/courses/30/lessons/42579
코딩테스트 연습 - 베스트앨범
스트리밍 사이트에서 장르 별로 가장 많이 재생된 노래를 두 개씩 모아 베스트 앨범을 출시하려 합니다. 노래는 고유 번호로 구분하며, 노래를 수록하는 기준은 다음과 같습니다. 속한 노래가
programmers.co.kr
문제해결방법
장르를 key, <고유번호, 재생횟수>를 저장하는 우선순위 큐를 value로 하는 HashMap과
장르를 key, 장르의 재생횟수를 value로 하는 HashMap 두 개를 사용해서 문제를 풀었다.
코드
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 | package Programmers; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.PriorityQueue; class Music implements Comparable<Music> { String genre; int count; Music(String genre, int count) { this.genre = genre; this.count = count; } @Override public int compareTo(Music arg0) { return Integer.compare(arg0.count, this.count); } } public class Solution_베스트앨범 { public static int[] solution(String[] genres, int[] plays) { Map<String, PriorityQueue<int[]>> musicByGenres = new HashMap<>(); Map<String, Integer> numberOfPlays = new HashMap<>(); for(int i=0; i<genres.length; i++) { if(numberOfPlays.containsKey(genres[i])) { numberOfPlays.put(genres[i], numberOfPlays.get(genres[i])+plays[i]); } else numberOfPlays.put(genres[i], plays[i]); if(musicByGenres.containsKey(genres[i])) { musicByGenres.get(genres[i]).add(new int[]{i, plays[i]}); } else { musicByGenres.put(genres[i], new PriorityQueue<int[]>(new Comparator<int[]>() { @Override public int compare(int[] arg0, int[] arg1) { if(arg0[1]==arg1[1]) return Integer.compare(arg0[0], arg1[0]); return Integer.compare(arg1[1], arg0[1]); } })); musicByGenres.get(genres[i]).add(new int[]{i, plays[i]}); } } PriorityQueue<Music> pq = new PriorityQueue<>(); for(String key : numberOfPlays.keySet()) { pq.add(new Music(key, numberOfPlays.get(key))); } List<Integer> list = new ArrayList<>(); while(!pq.isEmpty()) { Music cur = pq.poll(); String genre = cur.genre; PriorityQueue<int[]> q = musicByGenres.get(genre); int i=0; while(!q.isEmpty() && i<2) { list.add(q.poll()[0]); i++; } } int[] answer = new int[list.size()]; for(int i=0; i<answer.length; i++) answer[i] = list.get(i); return answer; } public static void main(String[] args) { String[] genres = {"classic", "pop", "classic", "classic", "pop"}; int[] plays = {500, 600, 150, 800, 2500}; System.out.println(Arrays.toString(solution(genres, plays))); } } | cs |
코드에 대한 피드백이나 더 좋은 아이디어는 언제나 환영입니다.
'개발 > 문제해결' 카테고리의 다른 글
[프로그래머스] 셔틀버스 (0) | 2021.01.23 |
---|---|
[프로그래머스] 가사 검색 (0) | 2021.01.21 |
[프로그래머스] 괄호 변환 (0) | 2021.01.20 |
[백준] 1194번 : 달이 차오른다, 가자. (0) | 2021.01.20 |
[프로그래머스] 블록 이동하기 (0) | 2021.01.14 |