์ฒด๊ฐ Level : โ
โ
โ
Review: ๋์ ๋ฐฐ์ด๊ณผ ๋ฐฐ์ด index ํ์ฉํ๊ธฐ |
๐ก์์ด ์ ์์ ์ํ ์ ์์ ํ๊ท ์ ์๋ฅผ ๊ธฐ์ค์ผ๋ก ํ์๋ค์ ๋ฑ์๋ฅผ ๋งค๊ธฐ๋ ค๊ณ ํฉ๋๋ค. ์์ด ์ ์์ ์ํ ์ ์๋ฅผ ๋ด์ 2์ฐจ์ ์ ์ ๋ฐฐ์ด score๊ฐ ์ฃผ์ด์ง ๋, ์์ด ์ ์์ ์ํ ์ ์์ ํ๊ท ์ ๊ธฐ์ค์ผ๋ก ๋งค๊ธด ๋ฑ์๋ฅผ ๋ด์ ๋ฐฐ์ด์ return
https://school.programmers.co.kr/learn/courses/30/lessons/120882
1) ๊ฐ๊ฐ ๋ํ ์ ์๋ฅผ ๋ฐฐ์ด์ ์ฝ์
2) ๋ด๋ฆผ์ฐจ์์ผ๋ก ์ ๋ ฌ (๋ํ๊ฐ์ด ์์ ์๋ก ์์๊ฐ ๋ฎ๋ค)
3) ์ ๋ฐฐ์ด์ ์์ด,์ํ ๊ณผ๋ชฉ ํด๋น ์ธ๋ฑ์ค๊ฐ + 1 (๋ฑ์)
[์ฐธ๊ณ ]
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;
}
}
[์คํจ] 33.3%
๊ฐ์ ๊ฐ ๋์ฌ ๋ ์ฒ๋ฆฌ๊ฐ ์๋จ
import java.util.Arrays;
import java.util.ArrayList;
class Solution {
public int[] solution(int[][] score) {
//ArrayList<Integer> answer = new ArrayList<Integer>();
int[] answer = new int[score.length];
int[] tmp = new int [score.length];
int[][] tmp2 = new int [score.length][score[0].length];
for (int i =0; i<score.length; i++){
tmp2[i][0] = i ;
tmp2[i][1] = (score[i][0]+score[i][1]) / 2;
tmp[i] = (score[i][0]+score[i][1]) / 2;
}
Arrays.sort(tmp);
int cnt = 1;
for (int i =score.length-1; i >= 0; i--){
for (int j =0; j < tmp2.length; j++){
if (tmp2[j][1] == tmp[i]){
answer[tmp2[j][0]] = cnt;
cnt++;
}
}
}
return answer;
}
}
[์คํจ]
ํด์ฌ๋งต์ ์ฌ์ฉํ๋ฉด ์๋จ... ๋ค๋ฅธ ๊ฐ์ด ๋ค์ด์ค๋ฉด ์ธ๋ฑ์ค๊ฐ ์ฐพ๊ธฐ๊ฐ ํ๋ค์ด์ง๋.
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
class Solution {
public int[] solution(int[][] score) {
ArrayList<Integer> answer = new ArrayList<Integer>();
Map<Integer,Integer> m = new HashMap<>();
int[] tmp = new int [score.length];
int sum1 = 0;
int sum2 = 0;
for (int i =0; i<score.length; i++){
tmp[i] = (score[i][0]+score[i][1]) / 2 ;
m.put((score[i][0]+score[i][1]) / 2, i) ;
}
//Arrays.sort(tmp);
//tmp ํ๊ท
//m <ํ๊ท , ์ธ๋ฑ์ค>
// 55-3,65-4,70-2,75-1
for (int i = 0; i < tmp.length; i++){
answer.add(m.get(tmp[i]));
}
return tmp;
}
}