๐Ÿ”’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 ์ด๋ ‡๊ฒŒ  ๊ตฌ์กฐ ๋ถ„ํ•ด ํ• ๋‹นํ•จ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ด๋Š” ํ…Œํฌ๋‹‰์ด ์ธ์ƒ ๊นŠ์—ˆ๋‹ค.