๐ด ๋ฌธ์ ์ค๋ช
๋ฐฐ์ด 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์ ๋๋ค.
๐ต ๋ด ์ฝ๋

function solution(array, commands) {
var answer = [];
commands.forEach(item => answer.push(
array.slice(item[0] - 1, item[1])
.sort((a, b) => a - b)[item[2] - 1]));
return answer;
}
๐ฃ ์ธ๊ธฐ ์ฝ๋

function solution(array, commands) {
return commands.map(command => {
const [start, end, position] = command
const newArray = array
.filter((value, fIndex) => fIndex >= start - 1 && fIndex <= end - 1)
.sort((a, b) => a - b)
return newArray[position - 1]
})
}
'Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ 1๋จ๊ณ ] ๋ก๋์ ์ต๊ณ ์์์ ์ต์ ์์ (0) | 2022.08.04 |
---|---|
[ 1๋จ๊ณ ] ์ฒด์ก๋ณต (0) | 2022.08.04 |
[ 1๋จ๊ณ ] ํฌ๋ ์ธ ์ธํ๋ฝ๊ธฐ ๊ฒ์ (0) | 2022.08.04 |
[ 1๋จ๊ณ ] ๋ชจ์๊ณ ์ฌ (0) | 2022.08.04 |
[ 1๋จ๊ณ ] ์์ฃผํ์ง ๋ชปํ ์ ์ (0) | 2022.07.25 |