본문 바로가기
TIL

프로그래머스 : 베스트 앨범

by 도쿠니 2022. 4. 7.

이런 문제 너무 재밌다..

제로베이스 연습문제였는데 이렇게 따로 클래스를 선언해서 풀었다.

따로 클래스에서 처리해줄거 처리해주니 편하게 로직을 작성할 수 있었다.

아래는 그냥 일반 실행용 코드

import java.util.*;

class Music {
    private int id;
    private int num;


    public Music(int id, int num) {
        this.id = id;
        this.num = num;
    }

    public int getId() {
        return id;
    }

    public int getNum() {
        return num;
    }
}

class Genre {
    private int total = 0;
    private ArrayList<Music> musics = new ArrayList<>();
    private String name;

    public Genre(String name) {
        this.name = name;
    }

    public void addMusic(Music music) {
        this.musics.add(music);
        this.total += music.getNum();
        this.musics.sort(Comparator.comparing(Music::getNum).reversed().thenComparing(Music::getId));
    }

    public int getTotal() {
        return total;
    }

    public ArrayList<Music> getMusics() {
        return musics;
    }

    public String getName() {
        return name;
    }
}


class Main {
    public static void main(String[] args) {
        int[] solution = solution(new String[]{"classic", "pop", "classic", "classic", "pop"}, new int[]{500, 600, 150, 800, 2500});
        System.out.println(Arrays.toString(solution));

    }

    public static int[] solution(String[] genres, int[] plays) {
        ArrayList<Integer> answer = new ArrayList<Integer>();
        HashMap<String, Genre> map = new HashMap<>();
        ArrayList<Genre> genreList = new ArrayList<>();

        for (int i = 0; i < genres.length; i++) {
            Genre genre = null;
            if (!map.containsKey(genres[i])) {
                genre = new Genre(genres[i]);

            } else {
                genre = map.get(genres[i]);
            }
            genre.addMusic(new Music(i, plays[i]));
            map.put(genres[i], genre);
        }
        for (Genre genre : map.values()) {
            genreList.add(genre);
        }

        genreList.sort(Comparator.comparing(Genre::getTotal).reversed());

        for (Genre genre : genreList) {
            ArrayList<Music> list = genre.getMusics();
            int n = list.size()<2 ? 1 : 2;
            for (int i = 0; i < n; i++) {
                int id = list.get(i).getId();
                answer.add(id);
            }
        }

        return answer.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

아래는 프로그래머스 제출용 코드

import java.util.*;
class Music {
    private int id;
    private int num;


    public Music(int id, int num) {
        this.id = id;
        this.num = num;
    }

    public int getId() {
        return id;
    }

    public int getNum() {
        return num;
    }
}

class Genre {
    private int total = 0;
    private ArrayList<Music> musics = new ArrayList<>();
    private String name;

    public Genre(String name) {
        this.name = name;
    }

    public void addMusic(Music music) {
        this.musics.add(music);
        this.total += music.getNum();
        this.musics.sort(Comparator.comparing(Music::getNum).reversed().thenComparing(Music::getId));
    }

    public int getTotal() {
        return total;
    }

    public ArrayList<Music> getMusics() {
        return musics;
    }

    public String getName() {
        return name;
    }
}
class Solution {
     public int[] solution(String[] genres, int[] plays) {
        ArrayList<Integer> answer = new ArrayList<Integer>();
        HashMap<String, Genre> map = new HashMap<>();
        ArrayList<Genre> genreList = new ArrayList<>();

        for (int i = 0; i < genres.length; i++) {
            Genre genre = null;
            if (!map.containsKey(genres[i])) {
                genre = new Genre(genres[i]);

            } else {
                genre = map.get(genres[i]);
            }
            genre.addMusic(new Music(i, plays[i]));
            map.put(genres[i], genre);
        }
        for (Genre genre : map.values()) {
            genreList.add(genre);
        }

        genreList.sort(Comparator.comparing(Genre::getTotal).reversed());

        for (Genre genre : genreList) {
            ArrayList<Music> list = genre.getMusics();
            int n = list.size()<2 ? 1 : 2;
            for (int i = 0; i < n; i++) {
                int id = list.get(i).getId();
                answer.add(id);
            }
        }

        return answer.stream().mapToInt(Integer::intValue).toArray();
    }
}

 

 

댓글