Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- array
- Binary Search
- Class
- database
- SQL
- sorting
- Matrix
- Binary Tree
- implement
- 코테
- 자바
- Counting
- string
- 코딩테스트
- Tree
- greedy
- Stack
- Method
- hash table
- two pointers
- Number Theory
- 구현
- geometry
- bit manipulation
- dynamic programming
- Data Structure
- Math
- simulation
- java
- 파이썬
Archives
- Today
- Total
코린이의 소소한 공부노트
[프로그래머스/Lv.0] 등수 매기기 본문
1. Input, Output, Example
- 학생들의 영어 점수와 수학 점수를 차례대로 담은 배열이 주어졌을 때, 영어 점수와 수학 점수의 평균을 기준으로 매긴 등수를 담은 배열을 반환
2. Constraint
1) 0 ≤ score[0], score[1] ≤ 100
2) 1 ≤ score의 길이 ≤ 10
3) score의 원소 길이는 2이다.
4) score는 중복된 원소를 갖지 않는다.
3. Code
1) 첫 코드(2023/05/30)
import java.util.*;
class Solution {
public int[] solution(int[][] score) {
int[] ans = new int[score.length];
float[] avg = new float[score.length];
HashMap<Float,Integer> m = new HashMap<>();
for(int i=0 ; i<score.length ; i++){
avg[i] = (score[i][0]+score[i][1])/2.0f;
m.put(avg[i], m.getOrDefault(avg[i],0)+1);
}
List<Float> list = new ArrayList<>(m.keySet());
list.sort(Comparator.naturalOrder());
int rank = 1;
for(int j=list.size()-1 ; j>=0 ; j--){
int count = 0; float max = list.get(j);
for(int i=0 ; i<avg.length && count<=m.get(max) ; i++)
if(avg[i]==max){
ans[i] = rank;
count++;
}
rank += m.get(max);
}
return ans;
}
}
2) 다른 사람의 풀이를 보던 중 가장 마음에 들었던 것(2023/05/30)
import java.util.*;
class Solution {
public int[] solution(int[][] score) {
List<Integer> scoreList = new ArrayList<>();
for(int[] t : score){
scoreList.add(t[0] + t[1]);
}
scoreList.sort(Comparator.reverseOrder());
int[] answer = new int[score.length];
for(int i=0; i<score.length; i++){
answer[i] = scoreList.indexOf(score[i][0] + score[i][1])+1;
}
return answer;
}
}
- 평균이 중복돼도 indexOf()는 가장 작은 인덱스가 나오므로 상관없다.
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 783. Minimum Distance Between BST Nodes (0) | 2023.05.31 |
---|---|
[프로그래머스/Lv.0] 저주의 숫자 3 (0) | 2023.05.30 |
[LeetCode/Easy] 706. Design HashMap (0) | 2023.05.30 |
[LeetCode/Easy] 703. Kth Largest Element in a Stream (0) | 2023.05.30 |
[LeetCode/Easy] 700. Search in a Binary Search Tree (0) | 2023.05.30 |