포스트

Hackerrank Left Rotation

Hackerrank Left Rotation

Left Rotation

문제 링크

설명

배열 요소를 주어진 만큼 왼쪽으로 회전시키면 된다. javascript Array의 내장함수로 쉽게 해결 할 수 있다.

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});

process.stdin.on("end", (_) => {
  inputString = inputString
    .replace(/\s*$/, "")
    .split("\n")
    .map((str) => str.replace(/\s*$/, ""));

  main();
});

function readLine() {
  return inputString[currentLine++];
}

function rotation(arr, d) {
  let rotated = arr;

  for (let i = 0; i < d; i++) {
    rotated.push(rotated.shift());
  }

  return rotated;
}

function main() {
  const nd = readLine().split(" ");

  const n = parseInt(nd[0], 10);

  const d = parseInt(nd[1], 10);

  const a = readLine()
    .split(" ")
    .map((aTemp) => parseInt(aTemp, 10));

  const result = rotation(a, d);

  console.log(result.join(" "));
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.