Java 로 풀어보는 알고리즘입니다. 📖
코딩테스트를 대비하여 JAVA1.8 부터 제공되는 함수형 API 는 사용하지 않았습니다.

문제 : https://www.algospot.com/judge/problem/read/MVP

 

algospot.com :: MVP

Most Valuable Programmer 문제 정보 문제 The season of professional programming league has just ended and it’s time for the MVP(Most Valuable Programmer) voting! There are n newspapers reporters who can vote, and the number of candidate programmers

www.algospot.com

 

풀이입니다. 🤔

 

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
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        int[][] prefers = new int[100][10];
        int[] votes = new int[10], positions = new int[100];
        boolean[] eliminated = new boolean[10];
        int cases = scanner.nextInt();
        while (cases-- > 0) {
            int n = scanner.nextInt(), m = scanner.nextInt();
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < m; ++j) {
                    prefers[i][j] = scanner.nextInt() - 1;
                }
            }
 
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    int prefer = -1;
                    while (positions[j] < m && prefer == -1) {
                        // 이 부분 구현을 위해 Queue 를 사용해도 좋다.
                        // 이 코드에서는 제거된 프로그래머를 제외하기 위해 prefers 의 Position 만을 제어하고 있다.
                        if (eliminated[prefers[j][positions[j]]]) ++positions[j];
                        else prefer = prefers[j][positions[j]];
                    }
 
                    if (prefer != -1++votes[prefer];
                }
 
                int minVote = Integer.MAX_VALUE, minCount = 0, clearedCount = 0;
                for (int j = 0; j < m; ++j) {
                    if (eliminated[j]) ++clearedCount;
                    else {
                        if (minVote == votes[j]) ++minCount;
                        else if (minVote > votes[j]) {
                            minCount = 1;
                            minVote = votes[j];
                        }
                    }
                }
                if (m - clearedCount != minCount) {
                    for (int j = 0; j < m; ++j) if (votes[j] == minVote) eliminated[j] = true;
                }
 
                Arrays.fill(votes, 0);
            }
 
            List<Integer> answers = new ArrayList<>();
            for (int i = 0; i < m; ++i) if (!eliminated[i]) answers.add(i + 1);
            for (int i = 0, size = answers.size(); i < size; ++i) {
                System.out.printf("%d%s", answers.get(i), (i + 1 == size) ? "\n" : " ");
            }
 
            Arrays.fill(eliminated, false);
            Arrays.fill(positions, 0);
        }
    }
}
cs

 

 

이 포스트를 읽어주셔서 감사합니다. 🙇🏻‍♂️

반응형

'개발이야기 > 알고스팟' 카테고리의 다른 글

[SPETIAL] Spatial Concepts Test  (0) 2022.01.09
[WORDLENGTH] 단어 길이 재기  (0) 2022.01.07
[MISPELL] Mispelling  (0) 2022.01.06
[DIAMOND] 다이아몬드  (0) 2022.01.06
[ENCODING] Encoding  (0) 2022.01.06
Posted by N'