์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 42748] K๋ฒˆ์งธ์ˆ˜ (Javascript)

์€์ง„ 2021. 6. 18. 16:49

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป๋ฌธ์ œ๋งํฌ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 42748] K๋ฒˆ์งธ์ˆ˜ (Javascript)
๋ฌธ์ œ ์„ค๋ช…
๋ฐฐ์—ด array์˜ i๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€ ์ž๋ฅด๊ณ  ์ •๋ ฌํ–ˆ์„ ๋•Œ, k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด array๊ฐ€ [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3์ด๋ผ๋ฉด

array์˜ 2๋ฒˆ์งธ๋ถ€ํ„ฐ 5๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฅด๋ฉด [5, 2, 6, 3]์ž…๋‹ˆ๋‹ค.
1์—์„œ ๋‚˜์˜จ ๋ฐฐ์—ด์„ ์ •๋ ฌํ•˜๋ฉด [2, 3, 5, 6]์ž…๋‹ˆ๋‹ค.
2์—์„œ ๋‚˜์˜จ ๋ฐฐ์—ด์˜ 3๋ฒˆ์งธ ์ˆซ์ž๋Š” 5์ž…๋‹ˆ๋‹ค.
๋ฐฐ์—ด array, [i, j, k]๋ฅผ ์›์†Œ๋กœ ๊ฐ€์ง„ 2์ฐจ์› ๋ฐฐ์—ด commands๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, commands์˜ ๋ชจ๋“  ์›์†Œ์— ๋Œ€ํ•ด ์•ž์„œ ์„ค๋ช…ํ•œ ์—ฐ์‚ฐ์„ ์ ์šฉํ–ˆ์„ ๋•Œ ๋‚˜์˜จ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ
array์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
array์˜ ๊ฐ ์›์†Œ๋Š” 1 ์ด์ƒ 100 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
commands์˜ ๊ธธ์ด๋Š” 1 ์ด์ƒ 50 ์ดํ•˜์ž…๋‹ˆ๋‹ค.
commands์˜ ๊ฐ ์›์†Œ๋Š” ๊ธธ์ด๊ฐ€ 3์ž…๋‹ˆ๋‹ค.
์ž…์ถœ๋ ฅ ์˜ˆ
array commands return
[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]
์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…
[1, 5, 2, 6, 3, 7, 4]๋ฅผ 2๋ฒˆ์งธ๋ถ€ํ„ฐ 5๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฅธ ํ›„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. [2, 3, 5, 6]์˜ ์„ธ ๋ฒˆ์งธ ์ˆซ์ž๋Š” 5์ž…๋‹ˆ๋‹ค.
[1, 5, 2, 6, 3, 7, 4]๋ฅผ 4๋ฒˆ์งธ๋ถ€ํ„ฐ 4๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฅธ ํ›„ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค. [6]์˜ ์ฒซ ๋ฒˆ์งธ ์ˆซ์ž๋Š” 6์ž…๋‹ˆ๋‹ค.
[1, 5, 2, 6, 3, 7, 4]๋ฅผ 1๋ฒˆ์งธ๋ถ€ํ„ฐ 7๋ฒˆ์งธ๊นŒ์ง€ ์ž๋ฆ…๋‹ˆ๋‹ค. [1, 2, 3, 4, 5, 6, 7]์˜ ์„ธ ๋ฒˆ์งธ ์ˆซ์ž๋Š” 3์ž…๋‹ˆ๋‹ค.

โœ๏ธIdea Sketch

2021-06-18

1. i~j๊นŒ์ง€ ๋ฐฐ์—ด ์ž๋ฅด๊ธฐ --> ์ •๋ ฌ --> K๋ฒˆ์งธ์ˆ˜
2. commands.length๋งŒํผ ๋ฐ˜๋ณต
commands.forEach(command => {
  let arr = array.splice().sort();
  result.push();
})
3. array.splice(index, ์ œ๊ฑฐํ•  ์š”์†Œ์˜ ๊ฐœ์ˆ˜, ์ถ”๊ฐ€ํ•  ์š”์†Œ)
  • ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์‚ญ์ œ/๊ต์ฒด/์ถ”๊ฐ€
  • ์›๋ณธ์ด ๋ฐ”๋€œ
const months = ['Jan', 'March', 'April', 'June'];

months.splice(1, 0, 'Feb');  // index 1์— 'Feb' ์ถ”๊ฐ€ (0๊ฐœ ์ œ๊ฑฐ)
console.log(months);  // output: ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');  // index 4๋ฅผ 'May'๋กœ ๋Œ€์ฒด (1๊ฐœ "June" ์ œ๊ฑฐ)
console.log(months);  // output: ["Jan", "Feb", "March", "April", "May"]
4. array.slice(begin, end)
  • ๋ฐฐ์—ด ์ธ๋ฑ์Šค begin๋ถ€ํ„ฐ end๊นŒ์ง€ ์–•์€ ๋ณต์‚ฌ๋ณธ์„ ๋ฐ˜ํ™˜
  • ๋ณต์‚ฌ๋‹ˆ๊นŒ ์›๋ณธ์€ ๋ฐ”๋€Œ์ง€ ์•Š์Œ
  • end ๋ฐ”๋กœ ์ „๊นŒ์ง€ ๋ณต์‚ฌํ•˜๋ฉฐ, end๊ฐ€ ์—†์œผ๋ฉด ๋๊นŒ์ง€ ๋ณต์‚ฌ
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];

console.log(animals.slice(2));  // output: ["camel", "duck", "elephant"]

console.log(animals.slice(2, 4));  // output: ["camel", "duck"] --> index 4์ธ 'elephant' ๋ฏธํฌํ•จ

console.log(animals.slice(1, 5));  // output: ["bison", "camel", "duck", "elephant"]

console.log(animals.slice(-2));  // output: ["duck", "elephant"]

console.log(animals.slice(2, -1));  // output: ["camel", "duck"] --> index -1์ธ 'elephant' ๋ฏธํฌํ•จ
5. ์›๋ณธ์ด ๋ฐ”๋€Œ๋ฉด ์•ˆ๋˜๋ฏ€๋กœ, array.slice() ์‚ฌ์šฉ

โœ๏ธ์†Œ์Šค์ฝ”๋“œ

2021-06-18 ์ •ํ™•์„ฑ ํ†ต๊ณผ

function solution(array, commands) {
  let res = [];
  commands.forEach(command => {
    let arr = array.slice(command[0] - 1, command[1]).sort((a, b) => a-b);  // i๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€ ์ž๋ฅด๊ณ  ์ •๋ ฌ
    res.push(arr[command[2] - 1])  // k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ
  })
  return res;
}

โœ๏ธ๋ช…๋‹ต

command[0], command[1], command[2] ๋ช…์‹œ์ ์œผ๋กœ ๋งŒ๋“ค๊ธฐ

const [sPosition, ePosition, position] = command
  • arr.map()์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”๋กœ return
  • ๋‚ด ์†Œ์Šค์ฝ”๋“œ์˜ res.push() ํ•„์š”x
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)  // i๋ฒˆ์งธ ์ˆซ์ž๋ถ€ํ„ฐ j๋ฒˆ์งธ ์ˆซ์ž๊นŒ์ง€ ์ž๋ฅด๊ธฐ
            .sort((a,b) => a - b)  // ์ •๋ ฌ

        return newArray[position - 1]  // k๋ฒˆ์งธ์— ์žˆ๋Š” ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ
    })
}