This is one of those problems where the clean idea matters more than the amount of code. For 189. Rotate Array, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- Naive solution, using extra array
- time: O(N), space: O(N)
Guide
When?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are rotate.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- The code keeps the moving pieces local, so the main idea stays visible.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Naive solution, using extra array
02//Runtime: 4 ms, faster than 99.57% of C++ online submissions for Rotate Array.
03//Memory Usage: 10.1 MB, less than 22.54% of C++ online submissions for Rotate Array.
04//time: O(N), space: O(N)
05class Solution {
06public:
07 void rotate(vector<int>& nums, int k) {
08 int n = nums.size();
09
10 vector<int> ans(n);
11
12 for(int i = 0; i < n; ++i){
13 ans[(i+k)%n] = nums[i];
14 }
15
16 for(int i = 0; i < n; ++i){
17 nums[i] = ans[i];
18 }
19 }
20};
21
22//Approach 1: Brute Force
23//time: O(N*k), space: O(1)
24//TLE
25//34 / 35 test cases passed.
26class Solution {
27public:
28 void rotate(vector<int>& nums, int k) {
29 int n = nums.size();
30
31 k %= n;
32
33 while(k-- > 0){
34 int prev = nums[n-1];
35 for(int i = 0; i < n; ++i){
36 swap(nums[i], prev);
37 }
38
39 // for(int i = 0; i < n; ++i){
40 // cout << nums[i] << " ";
41 // }
42 // cout << endl;
43 }
44 }
45};
46
47//Approach 3: Using Cyclic Replacements
48//Runtime: 8 ms, faster than 88.24% of C++ online submissions for Rotate Array.
49//Memory Usage: 9.9 MB, less than 70.95% of C++ online submissions for Rotate Array.
50//time: O(N), space: O(1)
51class Solution {
52public:
53 void rotate(vector<int>& nums, int k) {
54 int n = nums.size();
55 k %= n;
56
57 int count = 0;
58
59 for(int start = 0; count < n; ++start){
60 int cur = start;
61 //temporarily store the value k position before nums[next]
62 int prev = nums[cur];
63 do{
64 int next = (cur+k) % n;
65 // cout << "nums[" << next << "]: " << prev << endl;
66 swap(nums[next], prev);
67 cur = next;
68 ++count;
69 }while(cur != start);
70 }
71 }
72};
73
74//Approach 4: Using Reverse
75//Runtime: 8 ms, faster than 88.24% of C++ online submissions for Rotate Array.
76//Memory Usage: 10.1 MB, less than 37.73% of C++ online submissions for Rotate Array.
77//time: O(N), space: O(1)
78class Solution {
79public:
80 void rotate(vector<int>& nums, int k) {
81 reverse(nums.begin(), nums.end());
82 reverse(nums.begin(), nums.begin()+k);
83 reverse(nums.begin()+k, nums.end());
84 }
85};
Cost