기술 블로그

베스트앨범 본문

알고리즘 문제/Programmers

베스트앨범

parkit 2020. 6. 24. 18:11
728x90
반응형

https://programmers.co.kr/learn/courses/30/lessons/42579




제가 푼 이 코드가 정답이긴 하나 복잡하니 다른 블로그의 코드를 참고하세요.



후에 다시 풀 예정입니다.





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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.io.*;
import java.util.*;
 
public class Solution {
 
    static BufferedReader br;
    static BufferedWriter bw;
    static HashMap<String, Integer> hm = new HashMap<String, Integer>();
    static HashMap<String, Integer> Mark = new HashMap<String, Integer>();
    static Vector<Vector<pair>> v = new Vector<Vector<pair>>();
    static Vector<pair> vt = new Vector<pair>();
 
    static int[] solution(String[] genres, int[] plays) {
        int[] answer = {};
     
        int len = genres.length;
        
        for(int i=0; i<len; i++) {
            Vector<pair> temp = new Vector<pair>();
            v.add(temp);
        }
        
        for(int i=0, j=0; i<len; i++) {
            if(hm.containsKey(genres[i])) {
                hm.put(genres[i], hm.get(genres[i]) + plays[i]);
                int num = Mark.get(genres[i]);
                v.get(num).add(new pair(plays[i], i));
            }
            else {
                hm.put(genres[i], plays[i]);
                Mark.put(genres[i], j);
                v.get(j).add(new pair(plays[i], i));
                ++j;
            }
        }
        
        for(String s : hm.keySet()) {
            int num = Mark.get(s);
            int cnt = hm.get(s);         
            vt.add(new pair(cnt, num));
        }
        
        Collections.sort(vt, new Comparator<pair>() {
            public int compare(pair p1, pair p2) {
                if(p1.first < p2.first) {
                    return 1;
                }
                
                return -1;
            }
        });
 
        int vts = vt.size();
        
        System.out.println();
        
        for(int i=0; i<len; i++) {
            if(!v.get(i).isEmpty()) {
                Collections.sort(v.get(i), new Comparator<pair>() {
                    public int compare(pair p1, pair p2) {
                        if(p1.first > p2.first) {
                            return -1;
                        }
                        
                        if(p1.second > p2.second) {
                            return 1;
                        }
                            
                        return -1;
                    }
                });
 
                System.out.println("-----------------");
                for(pair p : v.get(i)) {
                    System.out.println(p.first + ", " + p.second);
                }
                System.out.println("-----------------");
            }
        }
        
        Vector<Integer> ret = new Vector<Integer>();
        
        int idx = 0;
        
        for(int i=0; i<vts; i++) {
            int number = vt.get(i).second;
            ret.add(v.get(number).get(0).second);
         
            if(v.get(number).size() >= 2) {
                ret.add(v.get(number).get(1).second);
            }
        }
        
        int rs = ret.size();
        answer = new int[rs];
        for(int i=0; i<rs; i++) {
            answer[i] = ret.get(i);
        }
        
        return answer;
    }
    
    public static void main(String[] args) throws IOException  {
        // TODO Auto-generated method stub
        br = new BufferedReader(new InputStreamReader(System.in));
        bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer st;
        //st = new StringTokenizer(br.readLine());
        //                      0         1      2            3        4      5
//        String[] genres = { "classic", "pop", "classic", "classic", "pop", "TEST" };
//        int[] plays = {800, 800, 800, 800, 800, 5000};
        
        //                      0       1          2           3       4
        String[] genres = { "classic""pop""classic""classic""pop" };
        int[] plays = {5006001508002500};
        
        // 답 : 5 0 2 1 4
        int[] ans = solution(genres, plays);
        
        for(int i=0; i<ans.length; i++) {
            System.out.print(ans[i] + " ");
        }
        
        bw.write("\n");
        bw.flush();
        bw.close();
    }
}
 
class pair {
    int first, second;
    pair(int a, int b) {
        this.first = a;
        this.second = b;
    }
}
 
class tuple {
    int first, second, third;
    tuple(int a, int b, int c) {
        this.first = a;
        this.second = b;
        this.third = c;
    }
}
 
class PQ implements Comparable<PQ> {
    int first, second;
    
    PQ(int f, int s) {
        this.first = f;
        this.second = s;
    }
    
    public int compareTo(PQ p) {
        if(this.first < p.first) {
            return -1// 오름차순
        }
        else if(this.first == p.first) {
            if(this.second < p.second) {
                return -1;
            }
        }
        
        return 1
        // 이미 this.first가 더 큰 것이 됐으므로, 1로 해준다.
        // -1로 하면 결과가 이상하게 출력됨.
    }
}
cs










728x90
반응형

'알고리즘 문제 > Programmers' 카테고리의 다른 글

스티커 모으기(2)  (0) 2020.06.26
올바른 괄호의 개수  (0) 2020.06.25
셔틀버스  (0) 2020.06.24
예산  (0) 2020.06.24
N으로 표현  (0) 2020.06.24