ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๋ฌธ์ ์ค๋ช
๋ฐฐ์ด array์ i๋ฒ์งธ ์ซ์๋ถํฐ j๋ฒ์งธ ์ซ์๊น์ง ์๋ฅด๊ณ ์ ๋ ฌํ์ ๋, k๋ฒ์งธ์ ์๋ ์๋ฅผ ๊ตฌํ๋ ค ํฉ๋๋ค.
์๋ฅผ ๋ค์ด array๊ฐ [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3์ด๋ผ๋ฉด
array์ 2๋ฒ์งธ๋ถํฐ 5๋ฒ์งธ๊น์ง ์๋ฅด๋ฉด [5, 2, 6, 3]์
๋๋ค.
1์์ ๋์จ ๋ฐฐ์ด์ ์ ๋ ฌํ๋ฉด [2, 3, 5, 6]์
๋๋ค.
2์์ ๋์จ ๋ฐฐ์ด์ 3๋ฒ์งธ ์ซ์๋ 5์
๋๋ค.
๋ฐฐ์ด array, [i, j, k]๋ฅผ ์์๋ก ๊ฐ์ง 2์ฐจ์ ๋ฐฐ์ด commands๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, commands์ ๋ชจ๋ ์์์ ๋ํด ์์ ์ค๋ช
ํ ์ฐ์ฐ์ ์ ์ฉํ์ ๋ ๋์จ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด์ ๋ด์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ์ฌํญ
array์ ๊ธธ์ด๋ 1 ์ด์ 100 ์ดํ์
๋๋ค.
array์ ๊ฐ ์์๋ 1 ์ด์ 100 ์ดํ์
๋๋ค.
commands์ ๊ธธ์ด๋ 1 ์ด์ 50 ์ดํ์
๋๋ค.
commands์ ๊ฐ ์์๋ ๊ธธ์ด๊ฐ 3์
๋๋ค.
์
์ถ๋ ฅ ์
array commands return
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
์
์ถ๋ ฅ ์ ์ค๋ช
[1, 5, 2, 6, 3, 7, 4]๋ฅผ 2๋ฒ์งธ๋ถํฐ 5๋ฒ์งธ๊น์ง ์๋ฅธ ํ ์ ๋ ฌํฉ๋๋ค. [2, 3, 5, 6]์ ์ธ ๋ฒ์งธ ์ซ์๋ 5์
๋๋ค.
[1, 5, 2, 6, 3, 7, 4]๋ฅผ 4๋ฒ์งธ๋ถํฐ 4๋ฒ์งธ๊น์ง ์๋ฅธ ํ ์ ๋ ฌํฉ๋๋ค. [6]์ ์ฒซ ๋ฒ์งธ ์ซ์๋ 6์
๋๋ค.
[1, 5, 2, 6, 3, 7, 4]๋ฅผ 1๋ฒ์งธ๋ถํฐ 7๋ฒ์งธ๊น์ง ์๋ฆ
๋๋ค. [1, 2, 3, 4, 5, 6, 7]์ ์ธ ๋ฒ์งธ ์ซ์๋ 3์
๋๋ค.
๋ก์ง
1. commands ํ์ ์ = answer ์์ ์
2. commands ๊ฐ ์ด(0,1,2)๋ ๊ฐ์ ์ญํ ์ ํ๋ ๋งค๊ฐ ๋ณ์
3. ๋ฐ๋ณต์์ ํจ์ํ
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
//commands ํ์ ๊ฐ์์ ๊ฐ์ ํฌ๊ธฐ์ ๋ฐฐ์ด answer ์ ์ธ
int[] answer = new int[commands.length];
//commands ๊ฐ ํx ๋ง๋ค
for(int x = 0; x<commands.length; x++){
int[] temp;
//๋ฐฐ์ด์ ์์ฑ
temp = Arrays.copyOfRange(array, commands[x][0]-1, commands[x][1]);
//์์ฑํ ๋ฐฐ์ด์ ์ ๋ ฌ
Arrays.sort(temp);
//n ๋ฒ์งธ์ ์์๋ฅผ answer์ ์์๋ก ๋ด์
answer[x] = temp[commands[x][2]-1];
}
return answer;
}
}
Arrays.sort() vs Collections.sort() ์ค ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ณ ๋ ค Collections.sort() ๊ฐ ๋ ์ ์ ?
2์ฐจ์ ๋ฐฐ์ด์ .length => ํ์ ๊ฐ์ ๋ฐํ