midnightly
[ํ๋ก๊ทธ๋๋จธ์ค] K๋ฒ์งธ์ (JAVA) ๋ณธ๋ฌธ
๐ ๋ฌธ์
๋ฐฐ์ด 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 ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
๐ ํ์ด
import java.util.*;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for (int i = 0; i < commands.length; i++) {
int[] tmp = new int[commands[i][1] - commands[i][0] + 1];
int cnt = 0;
for (int j = commands[i][0] - 1 ; j < commands[i][1] ; j++) {
tmp[cnt] = array[j];
cnt ++;
}
Arrays.sort(tmp);
answer[i] = tmp[commands[i][2] - 1];
}
return answer;
}
}
https://programmers.co.kr/learn/courses/30/lessons/42748
์ฝ๋ฉํ ์คํธ ์ฐ์ต - K๋ฒ์งธ์
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
programmers.co.kr
'Algorithm > programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ๋ํ๊ธฐ (JAVA) (0) | 2021.10.01 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ด์ (JAVA) (0) | 2021.10.01 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋ถ์กฑํ ๊ธ์ก ๊ณ์ฐํ๊ธฐ (JAVA) (0) | 2021.10.01 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ฐ์ด๋ฐ ๊ธ์ ๊ฐ์ ธ์ค๊ธฐ (JAVA) (0) | 2021.10.01 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฌ๋ฐ๋ฅธ ๊ดํธ (JAVA) (0) | 2021.10.01 |
Comments