๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

ํ”„์—” ๊ณต๋ถ€/๐Ÿซง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€

[๋ฐฑ์ค€/Node.js] 1157๋ฒˆ. ๋ฐ”๊ตฌ๋‹ˆ ์ˆœ์„œ ๋ฐ”๊พธ๊ธฐ || ํ’€์ด JavaScript

728x90

const input = require('fs').readFileSync('/dev/stdin').toString().split("\n").map(v => (v.replace("\r", "")));
const arr = input.map(v => v.split(" "));
const N = arr[0][0];
const M = arr[0][1];

let baskets = [];
let baskets2 = [];
for (let i = 1; i <= N; i++) {
    baskets.push(i);
    baskets2.push(i);
}
const rotateArr = (start, mid, end) => {
    let rotateNum = [];
    rotateNum = baskets.splice(start - 1, end - start + 1);
    const originMidIndex = baskets2[mid - 1];
    const newMidIndex = rotateNum.findIndex(v => v === originMidIndex);
    let midNums = rotateNum.splice(newMidIndex, rotateNum.length - newMidIndex);
    let finishedRotateNums = [...midNums, ...rotateNum];

    baskets2.splice(start - 1, end - start + 1, ...finishedRotateNums);
    return baskets2;
}

for (let i = 1; i <= M; i++) {
    let start = +arr[i][0];
    let end = +arr[i][1];
    let mid = +arr[i][2];

    baskets = rotateArr(start, mid, end).map(v => v);
    baskets2 = baskets.map(v => v);
}
console.log(...baskets);

 

์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ์ƒ๊ฐ์ด ์•ˆ๋‚˜๊ณ , ์ž˜ ์•ˆ๋ผ์„œ ํ’€๋‹ค ํ•œ๋ฒˆ ํฌ๊ธฐํ–ˆ์—ˆ๋‹ค. ๐Ÿคจ๐Ÿ˜ซ๐Ÿคฏ

 

 

๊ทธ๋Ÿฌ๋‹ค ์˜ค๋Š˜์€ splice ํ•จ์ˆ˜๊ฐ€ ์ƒ๊ฐ๋‚ฌ๋‹ค. 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

 

Array.prototype.splice() - JavaScript | MDN

splice() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๊ธฐ์กด ์š”์†Œ๋ฅผ ์‚ญ์ œ ๋˜๋Š” ๊ต์ฒดํ•˜๊ฑฐ๋‚˜ ์ƒˆ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

developer.mozilla.org

 

 

<splice ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•>

๋ฐฐ์—ด.splice( a , b, c );

a = ์‹œ์ž‘index

b = ์ž˜๋ผ๋‚ผ ๊ฐœ์ˆ˜ (0์„ ์“ฐ๋ฉด ์–ด๋–ค ์š”์†Œ๋„ ์ œ๊ฑฐ๋˜์ง€ ์•Š์Œ)

c = ์ž˜๋ผ๋‚ธ ๋ถ€๋ถ„์— ๋„ฃ์„ ์š”์†Œ (์•ˆ์“ฐ๋ฉด ์ œ๊ฑฐํ•˜๊ธฐ๋งŒ ํ•จ)

return ๊ฐ’: ์ž˜๋ผ๋‚ธ ๋ฐฐ์—ด.

splice ํ•œ ๋ฐฐ์—ด์€ ๋ณ€๊ฒฝ๋œ ์ƒํƒœ.

 

์˜ˆ์‹œ)

const food = ['๐Ÿ•', '๐ŸŸ', '๐Ÿฅ—', '๐Ÿข'];
food.splice(1, 0, '๐Ÿ€');
// index 1 ๋ถ€ํ„ฐ 0๊ฐœ ์‚ญ์ œ, ๊ทธ ์ž๋ฆฌ(index 1 ์ž๋ฆฌ)์— ๐Ÿ€ ์„ ๋„ฃ์Œ
console.log(food);
// ["๐Ÿ•", "๐Ÿ€", "๐ŸŸ", "๐Ÿฅ—", "๐Ÿข"] ๋ฐฐ์—ด ๋ณ€ํ™”๋จ. 
food.splice(1, 3);
// [ '๐Ÿ€', '๐ŸŸ', '๐Ÿฅ—'] //return ๊ฐ’์ž„. ๋ฐ”๋กœ ์œ„์˜ ๋ฐฐ์—ด์—์„œ index 1 ๋ถ€ํ„ฐ 3๊ฐœ ๋„๋ ค๋ƒ„
console.log(food);
// [ '๐Ÿ•', '๐Ÿข' ] //๋‚จ์€ ๋ฐฐ์—ด food ์˜ ์ƒํƒœ

 

 

 

<ํ’€์ด ๊ณผ์ •>

์ „์ฒด ๋ฐ”๊ตฌ๋‹ˆ ํ•จ์ˆ˜์—์„œ splice ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ํšŒ์ „ ์‹œํ‚ฌ ๋ถ€๋ถ„์„ ๋จผ์ € ๊ฐ€์ ธ์˜จ ํ›„,

๊ฐ€์ ธ์˜จ ๋ฐฐ์—ด์„ mid ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋ถ„๋ฆฌํ•ด์ค€ ๋‹ค์Œ์— ํ•ฉ์ณ์คฌ๋‹ค.

 

์ด ๋•Œ, ์ฃผ์–ด์ง„ mid ์˜ ์ˆ˜๋Š” ์›๋ž˜ ํ•จ์ˆ˜์—์„œ์˜ N๋ฒˆ ์งธ ๋ฐ”๊ตฌ๋‹ˆ์ด๊ธฐ ๋•Œ๋ฌธ์—, 

์›๋ž˜ baskets ๋ฐฐ์—ด์—์„œ, index mid -1 ์ด ์–ด๋–ค ์ˆซ์ž์ธ์ง€ ์ฐพ์€ ํ›„, 

ํšŒ์ „ ์‹œํ‚ค๋ ค๊ณ  ๋–ผ์–ด์ค€ ๋ฐฐ์—ด์—์„œ ๊ทธ ์ˆ˜์˜ index๋ฅผ ์ฐพ์•„ ์ค‘์‹ฌ์œผ๋กœ ์‚ผ์•˜๋‹ค. 

 

baskets๊ณผ ๋˜‘๊ฐ™์€ ๋ฐฐ์—ด baskets2 ๋ฅผ ๋งŒ๋“ค์–ด์คฌ๋‹ค.

๊ทธ๋ƒฅ ํ• ๋‹น์‹œ ์–•์€ ๋ณต์‚ฌ๊ฐ€ ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— map ํ•จ์ˆ˜๋กœ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ํ• ๋‹นํ•ด์คฌ๋‹ค. (๊ฐ™์€ ๋ฉ”๋ชจ๋ฆฌ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์Œ)

 

for ๋ฌธ์œผ๋กœ ์ด ๋ชจ๋“  ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„, 

๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ๋‚˜์˜ค๋Š” baskets ๋ฐฐ์—ด์„ ํŽผ์ณ์„œ ์ถœ๋ ฅํ–ˆ๋‹ค.

 

ํ’€์–ด๋‚ด์„œ ๋งค์šฐ ์†์‹œ์›ํ•˜๋‹ค! 

 

 

 

728x90
๋ฐ˜์‘ํ˜•