๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

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

 

์ด๋ ‡๊ฒŒ ๋†“๊ณ  ๋น„๊ตํ•˜๋‹ˆ๊นŒ ํ™•์‹คํžˆ ๋‹ค๋ฅด๊ธด ํ•˜๋„ค. 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€

๋Œ“๊ธ€์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.