Skip to content

Commit 2655eb1

Browse files
authored
Merge pull request #873 from mahek0620/house-robber
Added leetcode solution for 198
2 parents af22deb + c3359be commit 2655eb1

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
---
2+
id: rotate-array
3+
title: Rotate Array
4+
sidebar_label: 0189 Rotate Array
5+
tags:
6+
- Array
7+
- LeetCode
8+
- Java
9+
- Python
10+
- C++
11+
description: "This is a solution to the Rotate Array problem on LeetCode."
12+
---
13+
14+
## Problem Description
15+
16+
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
17+
18+
### Examples
19+
20+
**Example 1:**
21+
22+
```
23+
Input: nums = [1,2,3,4,5,6,7], k = 3
24+
Output: [5,6,7,1,2,3,4]
25+
Explanation:
26+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
27+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
28+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
29+
30+
```
31+
32+
**Example 2:**
33+
34+
```
35+
Input: nums = [-1,-100,3,99], k = 2
36+
Output: [3,99,-1,-100]
37+
Explanation:
38+
rotate 1 steps to the right: [99,-1,-100,3]
39+
rotate 2 steps to the right: [3,99,-1,-100]
40+
```
41+
42+
### Constraints
43+
44+
- $1 <= nums.length <= 105$
45+
- $-231 <= nums[i] <= 231 - 1$
46+
- $0 <= k <= 105$
47+
48+
49+
## Solution for Candy Distribution Problem
50+
51+
### Intuition And Approach
52+
53+
To rotate an array to the right by k steps, we need to move the last k elements to the front and shift the rest of the elements to the right. A straightforward way to achieve this in-place (without using extra space for another array) is to use the reversal method.
54+
55+
Here is the step-by-step approach:
56+
57+
## Adjust k:
58+
59+
If k is greater than the length of the array, rotating by k steps is the same as rotating by k % n steps (where n is the length of the array). This is because rotating by the length of the array brings it back to the original position.
60+
Calculate k = k % n.
61+
62+
## Reverse the entire array:
63+
64+
By reversing the entire array, the last k elements (which we want to move to the front) will be at the beginning, but in reverse order.
65+
For example, reversing [1, 2, 3, 4, 5, 6, 7] gives [7, 6, 5, 4, 3, 2, 1].
66+
67+
## Reverse the first k elements:
68+
69+
The first k elements are now the elements that were originally at the end of the array. Reverse these to restore their original order.
70+
Continuing the example, reversing the first 3 elements of [7, 6, 5, 4, 3, 2, 1] gives [5, 6, 7, 4, 3, 2, 1].
71+
72+
## Reverse the remaining n - k elements:
73+
74+
Finally, reverse the rest of the array (from the k-th element to the end) to restore their order.
75+
In the example, reversing the elements from index 3 to 6 of [5, 6, 7, 4, 3, 2, 1] gives [5, 6, 7, 1, 2, 3, 4].
76+
77+
78+
79+
#### Code in Different Languages
80+
81+
<Tabs>
82+
<TabItem value="Java" label="Java">
83+
<SolutionAuthor name="@mahek0620"/>
84+
```java
85+
86+
class Solution {
87+
public void rotate(int[] nums, int k) {
88+
// Ensure k is within the bounds of the array length
89+
k = k % nums.length;
90+
91+
// Reverse the entire array
92+
reverse(nums, 0, nums.length - 1);
93+
94+
// Reverse the first k elements
95+
reverse(nums, 0, k - 1);
96+
97+
// Reverse the remaining elements
98+
reverse(nums, k, nums.length - 1);
99+
}
100+
101+
// Helper function to reverse elements in the array from start to end
102+
private void reverse(int[] nums, int start, int end) {
103+
while (start < end) {
104+
int temp = nums[start];
105+
nums[start] = nums[end];
106+
nums[end] = temp;
107+
start++;
108+
end--;
109+
}
110+
}
111+
112+
```
113+
</TabItem>
114+
<TabItem value="Python" label="Python">
115+
<SolutionAuthor name="@mahek0620"/>
116+
```python
117+
118+
class Solution(object):
119+
def rotate(self, nums, k):
120+
k = k % len(nums)
121+
self.reverse(nums, 0, len(nums) - 1)
122+
self.reverse(nums, 0, k - 1)
123+
self.reverse(nums, k, len(nums) - 1)
124+
def reverse(self, nums, start, end):
125+
while start < end:
126+
nums[start], nums[end] = nums[end], nums[start]
127+
start += 1
128+
end -= 1
129+
```
130+
</TabItem>
131+
132+
<TabItem value="C++" label="C++">
133+
<SolutionAuthor name="@mahek0620"/>
134+
```cpp
135+
136+
#include <vector>
137+
using namespace std;
138+
139+
class Solution {
140+
public:
141+
void rotate(vector<int>& nums, int k) {
142+
// Ensure k is within the bounds of the array length
143+
k = k % nums.size();
144+
145+
// Reverse the entire array
146+
reverse(nums, 0, nums.size() - 1);
147+
148+
// Reverse the first k elements
149+
reverse(nums, 0, k - 1);
150+
151+
// Reverse the remaining elements
152+
reverse(nums, k, nums.size() - 1);
153+
}
154+
155+
private:
156+
void reverse(vector<int>& nums, int start, int end) {
157+
while (start < end) {
158+
int temp = nums[start];
159+
nums[start] = nums[end];
160+
nums[end] = temp;
161+
start++;
162+
end--;
163+
}
164+
}
165+
};
166+
```
167+
</TabItem>
168+
</Tabs>
169+
170+
171+
172+
## References
173+
174+
- **LeetCode Problem:** [House robber Problem](https://leetcode.com/problems/house-robber/)
175+
- **Solution Link:** [House Robber Solution on LeetCode](https://leetcode.com/problems/house-robber/solutions/5273312/house-robber-solution)
176+
- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/)

0 commit comments

Comments
 (0)