๋ฌธ์ ์ค๋ช
๋ฐฐ์ด 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 ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
๋ฌธ์ ์ฃผ์: https://programmers.co.kr/learn/courses/30/lessons/42748
๋ฌธ์ ํ์ด
array๋ฅผ i๋ถํฐ j๊น์ง ์๋ฅธ ๋ฐฐ์ด์ temp์ ์ ์ฅํ๊ณ , temp๋ฅผ ์ ๋ ฌํ ๋ค์ k๋ฒ์งธ์ ์๋ ์๋ฅผ answer์ push ํ๋ฉด ๋จ
์์ค ์ฝ๋
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
for (int i = 0; i < commands.size(); i++) {
vector<int> temp;
temp.assign(array.begin() + commands[i][0] - 1, array.begin() + commands[i][1]);
sort(temp.begin(), temp.end());
answer.push_back(temp[commands[i][2] - 1]);
}
return answer;
}
|
cs |
'Algorithm > ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค / C++] ๋ผ๋ฉด๊ณต์ฅ ํ์ด (0) | 2020.07.24 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค / C++] ๋ ๋งต๊ฒ ํ์ด (0) | 2020.07.24 |
[C++] ํ๋ก๊ทธ๋๋จธ์ค - "๋ชจ์๊ณ ์ฌ" ํ์ด (0) | 2020.07.03 |
[C++] ํ๋ก๊ทธ๋๋จธ์ค - "H-index" ํ์ด (0) | 2020.06.03 |
[C++] ํ๋ก๊ทธ๋๋จธ์ค - "๊ฐ์ฅ ํฐ ์" ํ์ด (0) | 2020.06.03 |