13. Rotate Right by K
A playlist is shown as a row of song IDs. Pressing the 'skip forward' button once moves the last song to the front of the list and pushes every other song one place to the right. The button is pressed k times in a row.
Write a method rotateRight that takes the array nums of song IDs and the integer k, and returns the playlist after k presses. Note that k can be much larger than the number of songs.
Example 1
- Input:
- nums = [1,2,3,4,5,6,7], k = 3
- Output:
- [5,6,7,1,2,3,4]
- Explanation:
After three presses the last three songs, 5, 6 and 7, have moved to the front: [5, 6, 7, 1, 2, 3, 4].
Example 2
- Input:
- nums = [10,20], k = 5
- Output:
- [20,10]
- Explanation:
With two songs, an odd number of presses swaps them, so pressing 5 times gives [20, 10].
Constraints
1 ≤ nums.length ≤ 1000
-109 ≤ nums[i] ≤ 109
0 ≤ k ≤ 105
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)