๐Algorithm
ํ๋ก๊ทธ๋๋จธ์ค ์ฝ๋ฉํ ์คํธ ํ์ด(js) > k๋ฒ์งธ์ (lv1)
devWarrior
2023. 7. 27. 21:00
๋ฌธ์ ๐ฝ
https://school.programmers.co.kr/learn/courses/30/lessons/42748
ํ๋ก๊ทธ๋๋จธ์ค
์ฝ๋ ์ค์ฌ์ ๊ฐ๋ฐ์ ์ฑ์ฉ. ์คํ ๊ธฐ๋ฐ์ ํฌ์ง์ ๋งค์นญ. ํ๋ก๊ทธ๋๋จธ์ค์ ๊ฐ๋ฐ์ ๋ง์ถคํ ํ๋กํ์ ๋ฑ๋กํ๊ณ , ๋์ ๊ธฐ์ ๊ถํฉ์ด ์ ๋ง๋ ๊ธฐ์ ๋ค์ ๋งค์นญ ๋ฐ์ผ์ธ์.
programmers.co.kr
๋ค๋ฅธ์ฌ๋ ํ์ด ๐ฝ
function solution(array, commands) {
return commands.map(command => {
const [sPosition, ePosition, position] = command
const newArray = array
.filter((value, fIndex) => fIndex >= sPosition - 1 && fIndex <= ePosition - 1)
.sort((a,b) => a - b)
return newArray[position - 1]
})
}
๋ดํ์ด๐ฝ
1. ์ฑ๊ณต โญ
function solution(array, commands) {
var answer = [];
commands.forEach((command,inedx)=>{
let t = array.slice(command[0]-1,command[1])
t.sort((a,b)=>a-b)
answer.push(t[command[2]-1])
})
return answer;
}
๋๋์ ๐ฝ
command[0] ,[1], [2] ์ด๋ ๊ฒ ์ฐ๋ ๊ฒ ๋ณด๋จ
let [sPosition, ePosition, position ] = command ์ด๋ ๊ฒ ๊ตฌ์กฐ ๋ถํด ํ ๋นํจ์ผ๋ก์จ ์ฝ๋์ ๊ฐ๋ ์ฑ์ ๋์ด๋ ํ ํฌ๋์ด ์ธ์ ๊น์๋ค.