문제
코딩테스트 연습 - 2주차
[[100,90,98,88,65],[50,45,99,85,77],[47,88,95,80,67],[61,57,100,80,65],[24,90,94,75,65]] "FBABD" [[70,49,90],[68,50,38],[73,31,100]] "CFD"
programmers.co.kr
풀이
Map의 getOrDefault 메소드를 이용하여 같은 점수가 여러개 존재하는지 알아냈고, TreeMap을 이용하여 최고점과 최저점을 알아냈다. TreeMap은 key값이 자동정렬되기 때문에 key를 점수로 설정하면 firstKey()와 lastKey()로 최저점과 최고점을 알아낼 수 있었기 때문이다.
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
|
import java.util.*;
class Solution {
public String solution(int[][] scores) {
StringBuilder answer = new StringBuilder();
for(int i = 0; i<scores[0].length; i++) {
int total = 0;
int person = scores[0].length;
TreeMap<Integer, Integer> hm = new TreeMap<>();
for(int j = 0; j<scores[i].length; j++) {
hm.put(scores[j][i], hm.getOrDefault(scores[j][i], 0) + 1);
total += scores[j][i];
}
if(scores[i][i] == hm.firstKey() && hm.get(hm.firstKey()) < 2) {
total -= hm.firstKey();
person -= 1;
}
if(scores[i][i] == hm.lastKey() && hm.get(hm.lastKey()) < 2) {
total -= hm.lastKey();
person -= 1;
}
answer.append(getGrade(total/person));
}
return answer.toString();
}
public String getGrade(double avg) {
if(avg >= 90) {
return "A";
}
else if(avg >= 80) {
return "B";
}
else if(avg >= 70) {
return "C";
}
else if(avg >= 50) {
return "D";
}
else
return "F";
}
}
|
cs |
아래는 좋아요를 가장 많이 받은 다른 사람의 풀이이다.
본인이 자기자신에게 매긴 점수를 제외하고 최고점과 최저점을 구했고, 구한 최저점과 최고점이 본인이 매긴 점수와 일치할 시에 제외하였다.
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
|
class Solution {
public String solution(int[][] scores) {
StringBuilder builder = new StringBuilder();
for(int i=0; i<scores.length; i++) {
int max = 0;
int min = 101;
int sum = 0;
int divide = scores.length;
for(int j=0; j<scores.length; j++) {
int score = scores[j][i];
if(i != j) {
if(score < min) {
min = score;
}
if(score > max) {
max = score;
}
}
sum += score;
}
if(scores[i][i] < min || scores[i][i] > max) {
sum -= scores[i][i];
divide--;
}
double score = (double) sum / divide;
builder.append(score >= 90 ? "A" : score >= 80 ? "B" : score >= 70 ? "C" : score >= 50 ? "D" : "F" );
}
return builder.toString();
}
}
|
cs |
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스/java] 큰수 만들기 (0) | 2023.07.14 |
---|