โค๏ธ Problem
- ๋ฌธ์
๋ฐฐ์ด array์ i๋ฒ์งธ ์ซ์๋ถํฐ j๋ฒ์งธ ์ซ์๊น์ง ์๋ฅด๊ณ ์ ๋ ฌํ์ ๋, k๋ฒ์งธ์ ์๋ ์๋ฅผ ๊ตฌํ๋ ค ํฉ๋๋ค.
์๋ฅผ ๋ค์ด array๊ฐ [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3์ด๋ผ๋ฉด
1. array์ 2๋ฒ์งธ๋ถํฐ 5๋ฒ์งธ๊น์ง ์๋ฅด๋ฉด [5, 2, 6, 3]์ ๋๋ค.
2. 1์์ ๋์จ ๋ฐฐ์ด์ ์ ๋ ฌํ๋ฉด [2, 3, 5, 6]์ ๋๋ค.
3. 2์์ ๋์จ ๋ฐฐ์ด์ 3๋ฒ์งธ ์ซ์๋ 5์ ๋๋ค.
๋ฐฐ์ด array, [i, j, k]๋ฅผ ์์๋ก ๊ฐ์ง 2์ฐจ์ ๋ฐฐ์ด commands๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋, commands์ ๋ชจ๋ ์์์ ๋ํด ์์ ์ค๋ช ํ ์ฐ์ฐ์ ์ ์ฉํ์ ๋ ๋์จ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด์ ๋ด์ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
- ์ ํ ์ฌํญ
- array์ ๊ธธ์ด๋ 1 ์ด์ 100 ์ดํ์ ๋๋ค.
- array์ ๊ฐ ์์๋ 1 ์ด์ 100 ์ดํ์ ๋๋ค.
- commands์ ๊ธธ์ด๋ 1 ์ด์ 50 ์ดํ์ ๋๋ค.
- commands์ ๊ฐ ์์๋ ๊ธธ์ด๊ฐ 3์ ๋๋ค.
- ์ ์ถ๋ ฅ ์ & ์ค๋ช
no | array | commands | return |
1 | [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์ ๋๋ค.
๐ Solution
ํ์ด 1
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i=0; i<commands.length; i++) {
int start = commands[i][0]-1;
int end = commands[i][1]-1;
int point = commands[i][2]-1;
int[] sorting = new int[end-start+1];
int index = 0;
for(int j=start; j<=end; j++) {
sorting[index] = array[j];
index++;
}
Arrays.sort(sorting);
answer[i] = sorting[point];
// System.out.println(answer[i]);
}
return answer;
}
}
์ฒ๋ฆฌ์๋ SO SO๐ค
ํ์ด 2
import java.util.Arrays;
class Solution {
public int[] solution(int[] array, int[][] commands) {
int[] answer = new int[commands.length];
for(int i=0; i<commands.length; i++) {
int start = commands[i][0]-1;
int end = commands[i][1];
int point = commands[i][2]-1;
int[] sorting = Arrays.copyOfRange(array, start, end);
Arrays.sort(sorting);
answer[i] = sorting[point];
System.out.println(answer[i]);
}
return answer;
}
}
์ฒ๋ฆฌ์๋ imporved?๐
๐ Comment
์ด ๋ฌธ์ ๋ ๋ฐฐ์ด์ ํน์ ๊ตฌ๊ฐ(commands[i][0]๋ถํฐ commands[i][1])์ ๊ฐ์ ์ถ์ถํ๋ ๊ฒ ํฌ์ธํธ์ธ ๊ฒ ๊ฐ๋ค. ๊ทธ๋ฆฌ๊ณ ํจ์ ์ด๋ผ๊ณ ํ๋ค๋ฉด ๋ฐฐ์ด์ n๋ฒ์งธ ์์น๋ก ์ฃผ์ด์ง๋ commands[i][2]๊ฐ ์ค์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค์ฒ๋ผ 0์ผ๋ก ์์ํ์ง ์๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ -1์ ํด์ฃผ์ด์ผ ํ๋ค๋ ์ ์ด๋ค.
ํ์ด 1์์๋ for ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํด์ int[] array์ commands[i][0]๋ถํฐ commands[i][1]๊น์ง์ ๋ฐฐ์ด์ int[] sorting์ผ๋ก ์ง์ ๋์ ํ๋ ๋ฐฉ์์ ์ฌ์ฉํ ๋ค, Array.sort()๋ฅผ ์ฌ์ฉํด์ ์ ๋ ฌํ์๋ค. ๊ทธ ๋ค์ commands[i][2]-1 ์ ํด๋นํ๋ ์์น์ sorting ๊ฐ์ ์ถ์ถํด์ int[] answer์ ์ง์ด ๋ฃ์ด์ ๋ฐํํ๋ฉด ๋!
ํ์ด 2์์๋ for ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ง์ ๋ฐฐ์ด์ ๋ณต์ฌํ์ง ์๊ณ Arrays.copyOfRange()๋ฅผ ์ฌ์ฉํด์ ๋ฐฐ์ด์ ๋ณต์ฌํ๋ ๋ฐฉ์์ ์ฌ์ฉํ๋ค. for ๋ฐ๋ณต๋ฌธ์ ์ฐ์ง ์์ผ๋ ์ฝ๋ ์ฑ๋ฅ์ด ์ฌ๋ผ๊ฐ์ง ์์๊น ๊ธฐ๋ํ๋๋ฐ ์๊ฐ๋ณด๋ค๋ ์ ์๋ฏธํ ์ฐจ์ด๊ฐ ์์ง ์์๋ค.
์บก์ณ๋ฅผ ๊น๋นกํ๋ค..๐๐ฆ 9069์!
'Programmers lv-1' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
2016๋ - Java [์ฝ๋ฉํ ์คํธ ์ฐ์ต] (0) | 2022.12.17 |
---|---|
๋ ๊ฐ ๋ฝ์์ ๋ํ๊ธฐ - Java [์ฝ๋ฉํ ์คํธ ์ฐ์ต] (0) | 2022.12.14 |
์ผ์ด์ฌ - Java [์ฝ๋ฉํ ์คํธ ์ฐ์ต] (0) | 2022.12.13 |
๋ฌธ์์ด ๋ด ๋ง์๋๋ก ์ ๋ ฌํ๊ธฐ - Java [์ฝ๋ฉํ ์คํธ ์ฐ์ต] (1) | 2022.12.12 |
์ต์์ง์ฌ๊ฐํ - Java [์ฝ๋ฉํ ์คํธ ์ฐ์ต] (0) | 2022.12.09 |
์์ ์ํธ - Java [์ฝ๋ฉํ ์คํธ ์ฐ์ต] (1) | 2022.12.08 |
๋๊ธ