You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/python/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 64 | 97.49
654
+
| 0033 |[Search in Rotated Sorted Array](src/main/python/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 36 | 91.71
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/python/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 64 | 97.49
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/python/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_5, Big_O_Time_O(log_n)_Space_O(1) | 64 | 97.49
Copy file name to clipboardExpand all lines: src/main/python/g0001_0100/s0031_next_permutation/readme.md
+61-58Lines changed: 61 additions & 58 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -37,63 +37,66 @@ The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algor
37
37
*`1 <= nums.length <= 100`
38
38
*`0 <= nums[i] <= 100`
39
39
40
-
To solve the "Next Permutation" problem in Java with a `Solution` class, we can follow these steps:
41
-
42
-
1. Define a `Solution` class.
43
-
2. Define a method named `nextPermutation` that takes an integer array `nums` as input and modifies it to find the next permutation in lexicographic order.
44
-
3. Find the first index `i` from the right such that `nums[i] > nums[i - 1]`. If no such index exists, reverse the entire array, as it's already the last permutation.
45
-
4. Find the smallest index `j` from the right such that `nums[j] > nums[i - 1]`.
46
-
5. Swap `nums[i - 1]` with `nums[j]`.
47
-
6. Reverse the suffix of the array starting from index `i`.
48
-
49
-
Here's the implementation:
50
-
51
-
```java
52
-
publicclassSolution {
53
-
publicvoidnextPermutation(int[] nums) {
54
-
int n = nums.length;
55
-
56
-
// Step 1: Find the first index i from the right such that nums[i] > nums[i - 1]
57
-
int i = n -1;
58
-
while (i >0&& nums[i] <= nums[i -1]) {
59
-
i--;
60
-
}
61
-
62
-
// Step 2: If no such index exists, reverse the entire array
63
-
if (i ==0) {
64
-
reverse(nums, 0, n -1);
65
-
return;
66
-
}
67
-
68
-
// Step 3: Find the smallest index j from the right such that nums[j] > nums[i - 1]
69
-
int j = n -1;
70
-
while (nums[j] <= nums[i -1]) {
71
-
j--;
72
-
}
73
-
74
-
// Step 4: Swap nums[i - 1] with nums[j]
75
-
swap(nums, i -1, j);
76
-
77
-
// Step 5: Reverse the suffix of the array starting from index i
78
-
reverse(nums, i, n -1);
79
-
}
80
-
81
-
// Helper method to reverse a portion of the array
// Helper method to swap two elements in the array
91
-
privatevoidswap(int[] nums, inti, intj) {
92
-
int temp = nums[i];
93
-
nums[i] = nums[j];
94
-
nums[j] = temp;
95
-
}
96
-
}
40
+
To solve the "Next Permutation" problem, you can use the following steps:
41
+
42
+
### Approach:
43
+
44
+
1.**Find the First Decreasing Element:**
45
+
- Starting from the right, find the first pair of adjacent elements `nums[i]` and `nums[i+1]` where `nums[i] < nums[i+1]`. This element `nums[i]` is the first decreasing element.
46
+
47
+
2.**Find the Smallest Element to the Right of `nums[i]`:**
48
+
- From the end, find the smallest element to the right of `nums[i]` that is greater than `nums[i]`. Let's call this element `nums[j]`.
49
+
50
+
3.**Swap `nums[i]` and `nums[j]`:**
51
+
- Swap the elements `nums[i]` and `nums[j]`.
52
+
53
+
4.**Reverse the Subarray to the Right of `nums[i]`:**
54
+
- Reverse the subarray to the right of `nums[i]` to ensure the smallest lexicographically next permutation.
55
+
56
+
### Python Code:
57
+
58
+
```python
59
+
classSolution:
60
+
defnextPermutation(self, nums):
61
+
# Step 1: Find the first decreasing element
62
+
i =len(nums) -2
63
+
while i >=0and nums[i] >= nums[i+1]:
64
+
i -=1
65
+
66
+
# Step 2: Find the smallest element to the right of nums[i] that is greater than nums[i]
67
+
if i >=0:
68
+
j =len(nums) -1
69
+
while nums[j] <= nums[i]:
70
+
j -=1
71
+
72
+
# Step 3: Swap nums[i] and nums[j]
73
+
nums[i], nums[j] = nums[j], nums[i]
74
+
75
+
# Step 4: Reverse the subarray to the right of nums[i]
This implementation provides a solution to the "Next Permutation" problem in Java. It finds the next lexicographically greater permutation of the given array `nums`and modifies it in place.
102
+
This code defines a `Solution` class with a method `nextPermutation` that takes a list of integers `nums` and modifies it to the next lexicographically greater permutation. The example usage demonstrates how to create an instance of the `Solution` class and call the `nextPermutation` method with different inputs.
0 commit comments