๋ฌธ์ ๋งํฌ
Java ํ์ด Step By Step์ผ๋ก ๋๋ ์ ํ์ด์ผํ๋ค.
-
์ฅ๋ฅด๋ณ ์ ์ฒด ํ๋ ์ด ์๋ฅผ ์ ์ฅํ ๊ฐ์ฒด๋ฅผ ๊ตฌํจ
{ classic: 1450, pop: 3100 }
-
์ฅ๋ฅด๋ณ๋ก ์ฅ๋ฅด์ ํ๋ ์ด์, ์ธ๋ฑ์ค๋ฅผ ์์๋ก ๊ฐ์ง๋ ๊ฐ์ฒด์ ๋ฐฐ์ด์ ๊ตฌํจ
{ classic: [ { genre: 'classic', play: 500, index: 0 }, { genre: 'classic', play: 150, index: 2 }, { genre: 'classic', play: 800, index: 3 } ], pop: [ { genre: 'pop', play: 600, index: 1 }, { genre: 'pop', play: 2500, index: 4 } ] }
-
1๋ฒ์์ ์ ์ํ ๊ฐ์ฒด๋ฅผ ์ฅ๋ฅด์ ํ๋ ์ด์๋ฅผ ์์๋ก ๊ฐ์ง๋ ๊ฐ์ฒด์ ๋ฐฐ์ด๋ก ๋ณ๊ฒฝํ๋ค.
[ { genre: 'classic', play: 1450 }, { genre: 'pop', play: 3100 } ]
-
3๋ฒ์์ ์ ์ํ ์ฅ๋ฅด๋ณ ์ ์ฒด ์นด์ดํธ ๋ฐฐ์ด์ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํ๋ค.
[ { genre: 'pop', play: 3100 }, { genre: 'classic', play: 1450 } ]
- 4๋ฒ์์ ์์ฑํ ๋ฐฐ์ด์ ๊ธฐ์ค์ผ๋ก ๋ฃจํ -> ์ฅ๋ฅด๋ฅผ ๊ธฐ์ค์ผ๋ก 2๋ฒ์์ ๋ฐฐ์ด์ ๊ฐ์ ธ์จ ํ, ๊ฐ์ ธ์จ ๋ฐฐ์ด์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌํ ๋ค์ ์์ 2๊ฐ๋ง PUSHํ๋ค.
function solution(genres, plays) {
var answer = [];
var songs = {};
var total = {};
var totalArr = [];
// ์ฅ๋ฅด๋ณ total play ๊ฐ์ฒด ์์ฑ + ์ฅ๋ฅด๋ณ๋ก ์ฅ๋ฅด์ ํ๋ ์ด์, ์ธ๋ฑ์ค๋ฅผ ์์๋ก ๊ฐ์ง๋ ๊ฐ์ฒด์ ๋ฐฐ์ด
for(var index = 0; index < genres.length; index++){
var genre = genres[index];
var play = plays[index];
if ( total[genre] === null || total[genre] === undefined ) {
total[genre] = play;
} else {
total[genre] += play;
}
if( songs[genre] === null || songs[genre] === undefined ){
songs[genre] = [];
}
songs[genre].push({
genre : genre,
play : play,
index : index
});
}
// ์ ๋ ฌ์ ์ํด ๋ฐฐ์ด๋ก ๋ณ๊ฒฝ
for(var key in total){
totalArr.push({
genre : key,
play : total[key]
});
}
// ์ ์ฒด ์นด์ดํธ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
totalArr.sort(function(a, b){
var aValue = a.play;
var bValue = b.play;
return bValue - aValue;
});
for(var index2 = 0; index2 < totalArr.length; index2++){
var thisGenre = totalArr[index2].genre;
var thisGenreArr = songs[thisGenre];
// ํ๋ ์ด ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
thisGenreArr.sort(function( a, b ) {
return b.play - a.play;
});
thisGenreArr[0] && answer.push(thisGenreArr[0].index);
thisGenreArr[1] && answer.push(thisGenreArr[1].index);
}
return answer;
}
JAVA ํ๊ธด ํ์๋๋ฐ ๋ฐํ์ ์ค๋ฅ๊ฐ ๋ฌ๋ค. ์ด์ ๋ฅผ ์์๊ฐ ์์ด์ ์ด๋ถ๊บผ ๋ณด๊ณ ์ฒ์๋ถํฐ ๋ค์ ์งฐ๋ค.
import java.util.*;
class Solution {
/*
class Genre {
String genre = "";
private int totalPlay = 0;
public Genre(String genre, int totalPlay) {
this.genre = genre;
this.totalPlay = totalPlay;
}
}
class Song {
private String genre = "";
private int play = 0;
private int index = 0;
public Song(String genre, int play, int index){
this.genre = genre;
this.play = play;
this.index = index;
}
}
public int[] solution(String[] genres, int[] plays) {
ArrayList<Genre> totalList = new ArrayList<Genre>();
Map<String, Integer> total = new HashMap<String, Integer>();
Map<String, ArrayList<Song>> songs = new HashMap<>();
for(int index = 0; index < genres.length; index++){
String genre = genres[index];
int play = plays[index];
if( genre != null ){
// System.out.println( genre );
if( !total.containsKey( genre ) ){
total.put(genre, play);
}else{
total.put(genre, total.get(genre) + play);
}
ArrayList<Song> songList;
if( !songs.containsKey(genre) ){
songList = new ArrayList<>();
}else{
songList = songs.get(genre);
}
songList.add(new Song(genre, play, index));
songs.put(genre, songList);
}
}
for(String genre : total.keySet()){
totalList.add(new Genre(genre, total.get(genre)));
}
Collections.sort(totalList, new Comparator<Genre>(){
@Override
public int compare(Genre o1, Genre o2) {
return o2.totalPlay - o1.totalPlay;
}
});
ArrayList<Integer> answerList = new ArrayList<>();
for(Genre genre : totalList){
ArrayList<Song> songList = (ArrayList<Song>) songs.get(genre.genre);
Collections.sort(songList, new Comparator<Song>(){
@Override
public int compare(Song o1, Song o2) {
return o2.play - o1.play;
}
});
for(int index = 0; index < 2; index++){
if( songList.get(index) != null ){
answerList.add(songList.get(index).index);
}
}
// answerList.add(songList.get(0).index);
// answerList.add(songList.get(1).index);
}
int[] answer = new int[answerList.size()];
for(int index = 0; index < answerList.size(); index++) {
answer[index] = answerList.get(index);
}
return answer;
}
*/
class Song {
private String genre = "";
private int play = 0;
private int index = 0;
public Song(String genre, int play, int index){
this.genre = genre;
this.play = play;
this.index = index;
}
}
public int[] solution(String[] genres, int[] plays) {
Map<String, Integer> total = new HashMap<String, Integer>();
ArrayList<Song> songs = new ArrayList<>();
for(int index = 0; index < genres.length; index++){
String genre = genres[index];
int play = plays[index];
if( genre != null ){
if( !total.containsKey( genre ) ){
total.put(genre, play);
}else{
total.put(genre, total.get(genre) + play);
}
songs.add(new Song(genre, play, index));
}
}
Collections.sort(songs, new Comparator<Song>(){
@Override
public int compare(Song o1, Song o2) {
if(o1.genre.equals(o2.genre)){
return o2.play - o1.play;
}else{
return total.get(o2.genre) - total.get(o1.genre);
}
}
});
Map<String, Integer> map = new HashMap<String, Integer>();
ArrayList<Integer> answerList = new ArrayList<>();
for(Song song : songs){
if( !map.containsKey(song.genre) ){
map.put(song.genre, 1);
answerList.add(song.index);
}else{
if( map.get(song.genre) < 2){
map.put(song.genre, map.get(song.genre) + 1);
answerList.add(song.index);
}
}
}
return answerList.stream().mapToInt(Integer::intValue).toArray();
}
}