42748. k๋ฒ์งธ ์
์ฝ๋ฉํ ์คํธ ์ฐ์ต - K๋ฒ์งธ์
| ๋ฌธ์ ์ ํ | ๋์ด๋ | ๊ฑธ๋ฆฐ ์๊ฐ | ํด๊ฒฐ ์ ๋ฌด(โ /โ) |
|---|---|---|---|
| ์ ๋ ฌ | lv.1 | 10๋ถ | โ |
์ค๊ณ ๋ฐฉ๋ฒ#
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ํจ์๋ก ์ฝ๊ฒ ํด๊ฒฐํจ .
์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์์์ง ๊ถ๊ธํจ.