Programmers - ๋ฒ ์ŠคํŠธ์•จ๋ฒ”

๋ฌธ์ œ ๋งํฌ

screenshot1 screenshot2

Java ํ’€์ด Step By Step์œผ๋กœ ๋‚˜๋ˆ ์„œ ํ’€์–ด์•ผํ•œ๋‹ค.

  1. ์žฅ๋ฅด๋ณ„ ์ „์ฒด ํ”Œ๋ ˆ์ด ์ˆ˜๋ฅผ ์ €์žฅํ•œ ๊ฐ์ฒด๋ฅผ ๊ตฌํ•จ

    { classic: 1450, pop: 3100 }
  2. ์žฅ๋ฅด๋ณ„๋กœ ์žฅ๋ฅด์™€ ํ”Œ๋ ˆ์ด์ˆ˜, ์ธ๋ฑ์Šค๋ฅผ ์š”์†Œ๋กœ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด์˜ ๋ฐฐ์—ด์„ ๊ตฌํ•จ

    {
    	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 }
    	]
    }
  3. 1๋ฒˆ์—์„œ ์ •์˜ํ•œ ๊ฐ์ฒด๋ฅผ ์žฅ๋ฅด์™€ ํ”Œ๋ ˆ์ด์ˆ˜๋ฅผ ์š”์†Œ๋กœ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด์˜ ๋ฐฐ์—ด๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

    [ { genre: 'classic', play: 1450 }, { genre: 'pop', play: 3100 } ]
  4. 3๋ฒˆ์—์„œ ์ •์˜ํ•œ ์žฅ๋ฅด๋ณ„ ์ „์ฒด ์นด์šดํŠธ ๋ฐฐ์—ด์„ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌํ•œ๋‹ค.

    [ { genre: 'pop', play: 3100 }, { genre: 'classic', play: 1450 } ]
  5. 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();
    }
}