[Programmers] #120882 - 등수 매기기 [Java][C++][Python]
[Programmers] #120882 - 등수 매기기 [Java][C++][Python]
1. 아이디어
등수는 자신보다 총점이 높은 학생의 수에 의해 결정된다. 각 학생에 대해 매번 score에서 자신보다 총점이 높은 학생의 수를 센 후 +1을 하면 공동 등수까지 깔끔하게 처리 가능하다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N^2)$ | $O(N)$ |
($N$ = score의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public int[] solution(int[][] score) {
int n = score.length;
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
int order = 1;
for (int j = 0; j < n; j++) {
if (score[i][0] + score[i][1] < score[j][0] + score[j][1]) order++;
}
ans[i] = order;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
vector<int> solution(vector<vector<int>> score) {
int n = score.size();
vector<int> ans(n);
for (int i = 0; i < n; i++) {
int order = 1;
for (int j = 0; j < n; j++) {
if (score[i][0] + score[i][1] < score[j][0] + score[j][1]) order++;
}
ans[i] = order;
}
return ans;
}
1
2
3
def solution(score):
total = [a + b for a, b in score]
return [1 + sum(t > s for t in total) for s in total]
This post is licensed under CC BY 4.0 by the author.