๐ก์ ๋ต
function solution(array, commands) {
var answer = [];
var temp;
for(var i=0; i<commands.length; i++){
temp = array.slice(commands[i][0]-1, commands[i][1]).sort((a,b)=> a-b);
answer.push(temp[commands[i][2]-1]);
}
return answer;
}
๐งํด์
1. ์ซ์๋ฅผ ์๋ฅธ๋ค slice()
2. ์ ๋ ฌํ๋ค sort()
3. ๋ต์ ๊ตฌํ๋ค push()
๐ JavaScript sort()ํจ์ ์ซ์ ํฌ๊ธฐ๋๋ก ์ ๋ ฌ
sort()ํจ์๋ ํ๋ผ๋ฏธํฐ๋ก compareFunction์ ๋ฐ๋๋ฐ ํจ์๊ฐ ์ ๋ ฅ๋์ง ์์ผ๋ฉด ์ ๋์ฝ๋ ์์์ ๋ฐ๋ผ ๊ฐ์ ์ ๋ ฌํ๋ค.
๋ฐ๋ผ์ ์ซ์์ ํฌ๊ธฐ ์์๋ก ์ ๋ ฌํ๋ ค๋ฉด sort((a, b) => a - b)๋ฅผ ์ ์ด์ค๋ค.
ex.sort(function(a, b) {
return a - b;
});
ex.sort((a, b) => a - b));
'์ฝํ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[14719]๋น๋ฌผ(JAVA, ์๊ณ ๋ฆฌ์ฆ, ํด์) (0) | 2021.04.28 |
---|---|
[2501]์ฝ์ ๊ตฌํ๊ธฐ(JAVA, JS, node) (0) | 2021.04.28 |
[python]์์ฃผํ์ง ๋ชปํ ์ ์ (0) | 2021.04.13 |
[SQL]ํ๋ก๊ทธ๋๋จธ์ค Level 1 (0) | 2021.04.13 |
[SQL] ํ๋ก๊ทธ๋๋จธ์ค (0) | 2021.04.13 |