๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

42748. k๋ฒˆ์งธ ์ˆ˜

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - K๋ฒˆ์งธ์ˆ˜


๋ฌธ์ œ ์œ ํ˜•๋‚œ์ด๋„๊ฑธ๋ฆฐ ์‹œ๊ฐ„ํ•ด๊ฒฐ ์œ ๋ฌด(โœ…/โŒ)
์ •๋ ฌlv.110๋ถ„โœ…

์„ค๊ณ„ ๋ฐฉ๋ฒ•#

  • commands ๋งˆ๋‹ค

  • commands์— ๋”ฐ๋ผ ๋ฐฐ์—ด์„ ์ž๋ฅด๊ณ 

  • sort ํ•จ์ˆ˜๋กœ ์ •๋ ฌํ•œ ๋’ค

  • ์ •๋ ฌํ•œ ๋ฐฐ์—ด์˜ command[2] - 1 ๋ฒˆ์งธ ์ˆ˜๋ฅผ

  • ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์— ๋‹ด๋Š”๋‹ค.

์ฝ”๋“œ#

function solution(array, commands) {    return commands.map(        (command) =>            array.slice(command[0] - 1, command[1]).sort((a, b) => a - b)[                command[2] - 1            ],    );}

์‹œ๊ฐ„ ๋ณต์žก๋„#

  • O(N*MlogM)

์–ด๋ ค์› ๋˜ ์ #

  • ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„์— ๋Œ€ํ•ด์„œ ๊ณ ๋ฏผํ•˜์ง€ ์•Š๊ณ  ๋‚ด์žฅ sortํ•จ์ˆ˜๋กœ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•จ .

  • ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„์ง€ ๊ถ๊ธˆํ•จ.

์ฐธ๊ณ ์ž๋ฃŒ#