https://programmers.co.kr/learn/courses/30/lessons/42748?language=javascript
๐ ๋ฌธ์
๐ ํ์ด
์ฌ์ฉํ ๋ฉ์๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
- slice(start, end): start ์ธ๋ฑ์ค๋ถํฐ end ์ธ๋ฑ์ค ์ด์ ๊น์ง ์ฌ๋ผ์ด์ฑํ ๋ฐฐ์ด ๋ฐํ
- sort: ์ ๋ ฌ๋ฉ์๋
- push: ๋ฐฐ์ด ์ค๋ฅธ์ชฝ์ผ๋ก ์์ ์ถ๊ฐ๋จ
์ฝ๋ ์์ฑ์ ์ด๋ ต์ง์์๋ฐ, sort() ์ ๋ ฌ๋ถ๋ถ์์ ์ฃผ์ํ ์ ์ด ์๋ค. ๋ง์ฝ ๋ค์๊ณผ๊ฐ์ด sort()๋ง ํด์ฃผ๋ฉด ์ค๋ฅ๊ฐ ๋ ๊ฒ์ด๋ค.
์๋ํ๋ฉด ์๋ฐ์คํฌ๋ฆฝํธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๋์ฝ๋ ๋ฌธ์์ด์ ๋น๊ตํด์ ์ ๋ ฌํ๊ธฐ ๋๋ฌธ์ ์ฐ๋ฆฌ๊ฐ ์ํ๋ ์ซ์ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ์ด ๋์ค์ง ์๊ฒ๋๋ค. ๋ฐ๋ผ์ sortํจ์ ์์ ์ ๋ ฌ์์๋ฅผ ์ ์ํด์ค์ผํ๋ค. ๋ค์๊ณผ ๊ฐ์ด ์์ ํด์ค๋ค.
์ ๋ ฌ๋ฌธ ์์ฑ์, ์๋ฐ์คํฌ๋ฆฝํธ๋ compareFunction์ ๊ผญ ๋ฐ๋ก ๋ง๋ค์ด์ค์ผํ๋ค!!
๐ ์ ์ฒด ์ฝ๋
function solution(array, commands) {
var answer = [];
for(var i=0; i<commands.length;i++){
var new_array = array.slice(commands[i][0]-1, commands[i][1]).sort((a, b) => {return a-b});
answer.push(new_array[commands[i][2]-1]);
}
return answer;
}
'๐ป๊ฐ๋ฐ๊ณต๋ถ > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ๋น๋ฐ์ง๋ Python (์นด์นด์ค ๊ธฐ์ถ) (0) | 2021.09.09 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ํํ Python (์นด์นด์ค ๊ธฐ์ถ) (0) | 2021.09.07 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์คํ์ฑํ ๋ฐฉ (Python) (0) | 2021.08.13 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๋๋จธ์ง ํ ์ (Python) (0) | 2021.06.24 |
๋ฐฑ์ค 9095: 1,2,3 ๋ํ๊ธฐ (DP) (0) | 2021.05.31 |