ํ๋ก๊ทธ๋๋จธ์ค: K๋ฒ์งธ ์
https://programmers.co.kr/learn/courses/30/lessons/4274
2019๋ ์ด ํ์ด
๋ด๊ฐ ํผ ๋ฐฉ๋ฒ์ด ํจ์จ์ ์ด๋ผ๊ณ ํ ์๋ ์๊ฒ ์ง๋ง, ์ด๋ ๊ฒ ํธ๋ ์ฌ๋๋ ์๋ค ์ ๋๋ก ์ฐธ๊ณ ๋ง ํ๋ฉด ์ข๊ฒ ๋ค.
๋๋ ์ฐ์ฐ(๋ฐฐ์ด์ ์๋ฅธ ํ ์ ๋ ฌ, n๋ฒ์งธ ์ ์ฐพ๊ธฐ)์ ์ํด์ ArrayList๋ฅผ ์ฌ์ฉํ๋ค. ์ด ๋ ArrayList ๋์ Vector ํน์ ์ผ๋ฐ ๋ฐฐ์ด์ ์ฌ์ฉํด๋ ์๊ด์๋ค.
import java.util.ArrayList; import java.util.Collections; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; ArrayList<Integer> temp = new ArrayList<>(); for (int i = 0; i < commands.length; i++) { for (int j = 0; j < commands[i].length; j++) { if (j == 0) { for (int k = commands[i][j] -1; k < commands[i][1]; k++) { temp.add(array[k]); } Collections.sort(temp); } } answer[i] = temp.get(commands[i][2] - 1); temp.clear(); } return answer; } }
์ด์์! ์๋ฃ๊ตฌ์กฐ์ ์๊ณ ๋ฆฌ์ฆ์ ์ฒ์์ด์ง? ์๊ฐ ํ ํ์ด
def solution(array, commands): answer = [] for i in commands: start = i[0] - 1 end = i[1] k = i[2] - 1 temp = array[start:end] temp.sort() answer.append(temp[k]) return answer
์ด๋ ๊ฒ ๋๊ณ ๋น๊ตํ๋๊น ํ์คํ ๋ค๋ฅด๊ธด ํ๋ค.
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote
๋๊ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.