๋ฌธ์ ์ค๋ช
๋ฐฐ์ด 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์
๋๋ค.
์ฝ๋
#include <string>
#include <vector>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int solution(int a, int b, int c, vector<int> arr) {
vector<int> temp;
int cnt = 1;
for(int i = a-1; i <= b-1; i++) {
temp.push_back(arr[i]);
}
int min;
for(int i = 0; i < temp.size(); i++) {
min = i;
for (int j = i; j < temp.size(); j++) {
if(temp[min] > temp[j]) {
min = j;
}
}
if(min!=i)
swap(temp[min], temp[i]);
}
return temp[(c-1)%(b-a+1)];
}
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
for(int i = 0; i < commands.size(); i++) {
answer.push_back(solution(commands[i][0],commands[i][1],commands[i][2], array));
}
return answer;
}
'๐ฅ๏ธ Computer > Algorithm Solution' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BAEKJOON] 2444๋ฒ : ๋ณ ์ฐ๊ธฐ-7 (0) | 2020.03.18 |
---|---|
[BAEKJOON] 2193๋ฒ : ์ด์น์ (1) | 2020.03.15 |
[BAEKJOON] 10773๋ฒ : ์ ๋ก (0) | 2020.03.08 |
[BAEKJOON] 2822๋ฒ : ์ ์๊ณ์ฐ (2) | 2020.03.01 |
[BAEKJOON] 1100๋ฒ : ํ์ ์นธ (0) | 2020.02.21 |
๋ฌธ์ ์ค๋ช
๋ฐฐ์ด 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์
๋๋ค.
์ฝ๋
#include <string>
#include <vector>
using namespace std;
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
int solution(int a, int b, int c, vector<int> arr) {
vector<int> temp;
int cnt = 1;
for(int i = a-1; i <= b-1; i++) {
temp.push_back(arr[i]);
}
int min;
for(int i = 0; i < temp.size(); i++) {
min = i;
for (int j = i; j < temp.size(); j++) {
if(temp[min] > temp[j]) {
min = j;
}
}
if(min!=i)
swap(temp[min], temp[i]);
}
return temp[(c-1)%(b-a+1)];
}
vector<int> solution(vector<int> array, vector<vector<int>> commands) {
vector<int> answer;
for(int i = 0; i < commands.size(); i++) {
answer.push_back(solution(commands[i][0],commands[i][1],commands[i][2], array));
}
return answer;
}
'๐ฅ๏ธ Computer > Algorithm Solution' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BAEKJOON] 2444๋ฒ : ๋ณ ์ฐ๊ธฐ-7 (0) | 2020.03.18 |
---|---|
[BAEKJOON] 2193๋ฒ : ์ด์น์ (1) | 2020.03.15 |
[BAEKJOON] 10773๋ฒ : ์ ๋ก (0) | 2020.03.08 |
[BAEKJOON] 2822๋ฒ : ์ ์๊ณ์ฐ (2) | 2020.03.01 |
[BAEKJOON] 1100๋ฒ : ํ์ ์นธ (0) | 2020.02.21 |