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
76
77
78
79
80
81
using System;
using System.Collections.Generic;
 
public class Solution
{
    //Music 구조체 및 생성자 구현
    public struct Music
    {
        public int number;
        public string genre;
        public int playCount;
 
        public Music(int numberValue, string genreValue, int playCountValue)
        {
            number = numberValue;
            genre = genreValue;
            playCount = playCountValue;
        }
    }
 
    //장르별 재생횟수 
    public Dictionary<stringint> genrePlayCountDic = new Dictionary<stringint>();
 
    //앨범에 포함된 장르의 곡 수
    public Dictionary<stringint> genreAlbumCountDic = new Dictionary<stringint>();
 
    public int[] solution(string[] genres, int[] plays)
    {
        //크기를 알고 있으니 배열로 선언
        Music[] musicArray = new Music[genres.Length];
 
        for (int i = 0; i < genres.Length; i++)
        {
            if (!genrePlayCountDic.ContainsKey(genres[i]))
                genrePlayCountDic[genres[i]] = 0;
 
            //뮤직 배열에 값을 담는다.
            Music music = new Music(i, genres[i], plays[i]);
            musicArray[i] = music;
 
            //장르별 재생 횟수를 담는다.
            genrePlayCountDic[genres[i]] += plays[i];
        }
 
        //문제에서 제시된 우선순위대로 정렬한다
        Array.Sort(musicArray, CompareMusic);
 
        List<int> answer = new List<int>();
 
        for(int i = 0; i < musicArray.Length; i++)
        {
            if (!genreAlbumCountDic.ContainsKey(musicArray[i].genre))
                genreAlbumCountDic[musicArray[i].genre] = 0;
 
            //장르별로 가장 많이 재생된 노래를 두개까지 넣을 수 있으므로 정답에서 제외
            if (genreAlbumCountDic[musicArray[i].genre] >= 2)
                continue;
 
            answer.Add(musicArray[i].number);
 
            //장르별로 앨범에 추가된 횟수를 담는다.
            genreAlbumCountDic[musicArray[i].genre]++;
        }
 
        return answer.ToArray();
    }
 
    private int CompareMusic(Music a, Music b)
    {
        //장르별 우선순위
        if (a.genre != b.genre)
            return genrePlayCountDic[b.genre] - genrePlayCountDic[a.genre];
 
        //재생횟수 우선순위
        if (a.playCount != b.playCount)
            return b.playCount - a.playCount;
 
        //번호 우선순위
        return a.number - b.number;
    }
}
cs

+ Recent posts