
๋ค์ ๋ฌธ์ ๋ฅผ ์๋์ ๊ฐ์ด ํ์ด๋ณด์๋ค.
import java.util.*;
import java.util.stream.Collectors;
class Solution {
public int[] solution(int[] emergency) {
List<Integer> sorted = Arrays.stream(emergency)
.sorted()
.boxed()
.collect(Collectors.toList());
int[] answer = new int[sorted.size()];
int idx = 0;
Collections.reverse(sorted);
for(int element : emergency) {
answer[idx++] = sorted.indexOf(element) + 1;
}
return answer;
}
}
1. Arrays.stream
์ ์ด์ฉํด int[]ํ์ IntStreamํ์ผ๋ก ๋ณํ
2. sorted()
๋ฅผ ์ด์ฉํด ๊ฐ์ ์ ๋ ฌ
3. boxed()
๋ฅผ ํตํด Stream<Integer>ํ์ผ๋ก ๋ณํ
4. collect(Collectors.toList())
๋ฅผ ํตํด ๋ฆฌ์คํธํ์ผ๋ก ๋ณํ
์์ ๊ณผ์ ์ ํตํด ์ ๋ ฌ๋ ๋ฆฌ์คํธ๋ฅผ ์ป์ ๋ค, reverse()
๋ฅผ ํตํด ๊ฐ์ด ์์์๋ก ํฐ ๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ง๋๋ก ํ์๋ค.
๊ทธ๋ ๊ฒ ์์ ๊ฐ์ ํตํด ์ธ๋ฑ์ค ๊ฐ์ ๋ฝ์๋ด์ด ์ฐ์ ์์๋ก ์ด์ฉํ ์ ์๊ฒ๋ ํ์ด๋ณด์๋๋ฐ, ํด๋น ๋ฌธ์ ์์๋ ์ค๋ณต๋ ์์๊ฐ ์๋ค๋ ์กฐ๊ฑด์ด ์์๊ธฐ์ ๊ฐ๋ฅํ ํ์ด์ด๊ธฐ๋ ํ๋ค.
์ฐธ๊ณ - ๋ค๋ฅธ ํ์ด