簡単 0件解決済み 正答率0%

Rotate Array

Rotate a list to the right by some steps.

このチャレンジで学べること

  • Shift elements with index arithmetic
  • Reduce large rotations with modulo
  • Build a rotated copy of a list

問題

翻訳を表示

Rotate the list to the right by steps positions: the last element moves to the front, steps times.

Implement rotate_right(numbers, steps).

Input

numbers: The list of integers. steps: How many positions to rotate.

Output

Return the rotated list.

制約

0 ≤ length ≤ 100,000; 0 ≤ steps ≤ 10⁹

例

例 1
入力:
numbers = [1, 2, 3, 4, 5]
steps = 2

期待される出力:
expected = [4, 5, 1, 2, 3]
入力:
std::vector<long long> numbers = {1, 2, 3, 4, 5};
long long steps = 2;

期待される出力:
std::vector<long long> expected = {4, 5, 1, 2, 3};
入力:
long[] numbers = new long[] {1, 2, 3, 4, 5};
long steps = 2;

期待される出力:
long[] expected = new long[] {4, 5, 1, 2, 3};

解説

Two right rotations: [5, 1, 2, 3, 4], then [4, 5, 1, 2, 3].

例 2
入力:
numbers = [1, 2, 3]
steps = 3

期待される出力:
expected = [1, 2, 3]
入力:
std::vector<long long> numbers = {1, 2, 3};
long long steps = 3;

期待される出力:
std::vector<long long> expected = {1, 2, 3};
入力:
long[] numbers = new long[] {1, 2, 3};
long steps = 3;

期待される出力:
long[] expected = new long[] {1, 2, 3};

解説

Rotating by the length brings the list back: [1, 2, 3].

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

A rotation by the list length changes nothing, so only the remainder matters.

ヒント 2

Think of splitting the list near the end and moving that tail to the front.

ヒント 3

Handle very large step counts by reducing them before rearranging.

目安の計算量

O(n) time, O(n) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。