Skip to content

Commit 30b24d6

Browse files
committed
fix 31 java
1 parent 7867ecf commit 30b24d6

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

leetcode_java/src/main/java/LeetCodeJava/Array/NextPermutation.java

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,91 @@ public class NextPermutation {
4646
// V0
4747
// IDEA : 2 POINTERS (gpt)
4848
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Array/next-permutation.py#L43
49+
/**
50+
* Examples :
51+
*
52+
*
53+
* ## Example 1
54+
*
55+
* **Input:** `nums = [1, 2, 3]`
56+
* **Initial State:** `nums = [1, 2, 3]`
57+
*
58+
* ### Step-by-Step Execution
59+
*
60+
* 1. **Find the first decreasing element from the end:**
61+
* - Start with `i = nums.length - 1 = 2`.
62+
* - Compare `nums[i - 1] = nums[1] = 2` with `nums[i] = nums[2] = 3`. Since `2 < 3`, stop. `i = 2`.
63+
*
64+
* 2. **Find the last "ascending" position:**
65+
* - `k = i - 1 = 1`.
66+
* - `nums[k] = nums[1] = 2`.
67+
*
68+
* 3. **Find the element just larger than `nums[k]`:**
69+
* - Start with `j = nums.length - 1 = 2`.
70+
* - Compare `nums[j] = nums[2] = 3` with `nums[k] = nums[1] = 2`. Since `3 > 2`, stop. `j = 2`.
71+
*
72+
* 4. **Swap `nums[k]` and `nums[j]`:**
73+
* - Swap `nums[1]` and `nums[2]`.
74+
* - After swap: `nums = [1, 3, 2]`.
75+
*
76+
* 5. **Reverse the sequence from `k + 1` to the end:**
77+
* - Subarray to reverse: `[2]`.
78+
* - No change since reversing a single element: `nums = [1, 3, 2]`.
79+
*
80+
* **Final Output:** `nums = [1, 3, 2]`
81+
*
82+
* ---
83+
*
84+
* ## Example 2
85+
*
86+
* **Input:** `nums = [3, 2, 1]`
87+
* **Initial State:** `nums = [3, 2, 1]`
88+
*
89+
* ### Step-by-Step Execution
90+
*
91+
* 1. **Find the first decreasing element:**
92+
* - Start with `i = nums.length - 1 = 2`.
93+
* - Compare `nums[1] = 2` with `nums[2] = 1`: `2 >= 1`, decrement `i`.
94+
* - Compare `nums[0] = 3` with `nums[1] = 2`: `3 >= 2`, decrement `i`.
95+
* - `i = 0`.
96+
*
97+
* 2. **Reverse the entire array:**
98+
* - Since `i == 0`, reverse the array: `nums = [1, 2, 3]`.
99+
*
100+
* **Final Output:** `nums = [1, 2, 3]`
101+
*
102+
* ---
103+
*
104+
* ## Key Points
105+
*
106+
* 1. The algorithm works in **O(n)** time.
107+
* 2. It rearranges the elements in-place using three main steps:
108+
* - Find the first decreasing element.
109+
* - Swap it with the next larger element.
110+
* - Reverse the sequence after the swapped position to get the next lexicographical order.
111+
*/
112+
/**
113+
* IDEA :
114+
*
115+
* Explanation of the Algorithm:
116+
*
117+
* 1. Identify the first decreasing element from the end:
118+
* • Traverse the array from the right and find the first index i such that nums[i] < nums[i + 1].
119+
* • This is the pivot point where the next permutation is possible.
120+
*
121+
* 2. Find the smallest element larger than nums[i] to the right of i:
122+
* • From the end of the array, find the first index j such that nums[j] > nums[i].
123+
*
124+
* 3. Swap nums[i] and nums[j]:
125+
* • Swap these elements to ensure the next permutation is lexicographically greater.
126+
*
127+
* 4. Reverse the suffix starting from i + 1:
128+
* • Reverse the elements to the right of i to ensure they are in ascending order, giving the smallest lexicographical order for the suffix.
129+
*
130+
* 5. If no i is found (i.e., the array is in descending order):
131+
* • Reverse the entire array to reset it to the smallest permutation.
132+
*
133+
*/
49134
public void nextPermutation(int[] nums) {
50135

51136
int i = nums.length - 1;

0 commit comments

Comments
 (0)