Skip to content

Commit fa9f728

Browse files
committed
update 31 java
1 parent 3e1d78a commit fa9f728

File tree

4 files changed

+166
-16
lines changed

4 files changed

+166
-16
lines changed

data/progress.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
20241122: 776
1+
20241122: 776,31
22
20241121: 004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)
33
20241120: 986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
44
20241117: 394

data/to_review.txt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
2025-01-16 -> ['776,31']
12
2025-01-15 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
23
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
34
2025-01-11 -> ['394']
@@ -7,30 +8,31 @@
78
2025-01-02 -> ['1188,130,855(again)']
89
2024-12-28 -> ['900']
910
2024-12-27 -> ['26,27', '802,1197,26']
11+
2024-12-26 -> ['776,31']
1012
2024-12-25 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
1113
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1214
2024-12-21 -> ['394', '855,846']
1315
2024-12-20 -> ['833,950', '932']
1416
2024-12-18 -> ['951,792']
1517
2024-12-14 -> ['53,210,207', '163,1048']
16-
2024-12-13 -> ['444', '298,729']
18+
2024-12-13 -> ['776,31', '444', '298,729']
1719
2024-12-12 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
1820
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1921
2024-12-08 -> ['394', '737']
2022
2024-12-07 -> ['833,950', '900', '686,734,737']
2123
2024-12-06 -> ['26,27', '802,1197,26', '353']
22-
2024-12-05 -> ['528,334']
24+
2024-12-05 -> ['776,31', '528,334']
2325
2024-12-04 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2426
2024-12-03 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2527
2024-12-01 -> ['53,210,207']
26-
2024-11-30 -> ['394', '444', '855,846', '1145,1219']
28+
2024-11-30 -> ['776,31', '394', '444', '855,846', '1145,1219']
2729
2024-11-29 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
2830
2024-11-28 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
29-
2024-11-27 -> ['951,792', '524,221,889']
31+
2024-11-27 -> ['776,31', '951,792', '524,221,889']
3032
2024-11-26 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
31-
2024-11-25 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
32-
2024-11-24 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
33-
2024-11-23 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '163,1048', '981']
33+
2024-11-25 -> ['776,31', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
34+
2024-11-24 -> ['776,31', '004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
35+
2024-11-23 -> ['776,31', '004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '163,1048', '981']
3436
2024-11-22 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '444', '298,729', '1087']
3537
2024-11-21 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '833,950', '1188,130,855(again)', '1146']
3638
2024-11-20 -> ['394', '939']
@@ -847,7 +849,7 @@
847849
2020-07-23 -> ['388']
848850
2020-07-22 -> ['269']
849851
2020-07-21 -> ['646', '537,553,579']
850-
2020-07-20 -> ['776', '210,261']
852+
2020-07-20 -> ['210,261', '776']
851853
2020-07-19 -> ['066,271', '361,393,133,207', '355,119,536']
852854
2020-07-18 -> ['734,737', '771,775', '451,414,189']
853855
2020-07-16 -> ['163', '762', '725,538,089']
@@ -859,26 +861,26 @@
859861
2020-07-10 -> ['734,737', '388', '836,860,863']
860862
2020-07-09 -> ['066,271', '694']
861863
2020-07-08 -> ['066,271', '163', '646', '663']
862-
2020-07-07 -> ['776', '066,271', '210,261', '298', '661,662', '703,787,819']
864+
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
863865
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
864866
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
865867
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
866868
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
867869
2020-07-02 -> ['210,261', '734,737', '298', '388', '742', '445,508']
868870
2020-07-01 -> ['130', '269', '210,261', '361,393,133,207', '322,380,394']
869871
2020-06-30 -> ['210,261', '361,393,133,207', '734,737', '346,686', '646', '738', '537,553,579', '289,295']
870-
2020-06-29 -> ['776', '361,393,133,207', '734,737', '298', '396', '251,287']
872+
2020-06-29 -> ['361,393,133,207', '734,737', '298', '776', '396', '251,287']
871873
2020-06-28 -> ['130', '734,737', '482,127,102,107', '355,119,536', '231,232,240']
872874
2020-06-27 -> ['298', '346,686', '388', '771,775', '451,414,189', '198,203,230']
873875
2020-06-26 -> ['130', '298', '694', '155,165,167,179']
874876
2020-06-25 -> ['130', '298', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
875-
2020-06-24 -> ['776', '130', '346,686', '388', '742', '661,662', '517,535,529', '153']
877+
2020-06-24 -> ['130', '346,686', '388', '776', '742', '661,662', '517,535,529', '153']
876878
2020-06-23 -> ['346,686', '482,127,102,107', '711,046,126,127', '131,134,150']
877879
2020-06-22 -> ['482,127,102,107', '388', '646', '771,775', '738', '701,450', '063,064,120,0146']
878-
2020-06-21 -> ['776', '482,127,102,107', '388', '396', '640,645', '675,297,138']
880+
2020-06-21 -> ['482,127,102,107', '388', '776', '396', '640,645', '675,297,138']
879881
2020-06-20 -> ['388', '646', '762', '606,459']
880-
2020-06-19 -> ['776', '646', '771,775', '742', '836,860,863']
881-
2020-06-18 -> ['776', '269', '646', '694']
882+
2020-06-19 -> ['646', '776', '771,775', '742', '836,860,863']
883+
2020-06-18 -> ['269', '646', '776', '694']
882884
2020-06-17 -> ['776', '771,775', '762', '738', '663', '537,553,579']
883885
2020-06-16 -> ['771,775', '742', '396', '661,662', '703,787,819']
884886
2020-06-15 -> ['771,775', '762', '355,119,536', '669,682,739,763']

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

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
11
package LeetCodeJava.Array;
22

33
// https://leetcode.com/problems/next-permutation/description/
4-
4+
/**
5+
* 31. Next Permutation
6+
* Solved
7+
* Medium
8+
* Topics
9+
* Companies
10+
* A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
11+
*
12+
* For example, for arr = [1,2,3], the following are all the permutations of arr: [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].
13+
* The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
14+
*
15+
* For example, the next permutation of arr = [1,2,3] is [1,3,2].
16+
* Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
17+
* While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
18+
* Given an array of integers nums, find the next permutation of nums.
19+
*
20+
* The replacement must be in place and use only constant extra memory.
21+
*
22+
*
23+
*
24+
* Example 1:
25+
*
26+
* Input: nums = [1,2,3]
27+
* Output: [1,3,2]
28+
* Example 2:
29+
*
30+
* Input: nums = [3,2,1]
31+
* Output: [1,2,3]
32+
* Example 3:
33+
*
34+
* Input: nums = [1,1,5]
35+
* Output: [1,5,1]
36+
*
37+
*
38+
* Constraints:
39+
*
40+
* 1 <= nums.length <= 100
41+
* 0 <= nums[i] <= 100
42+
*
43+
*/
544
public class NextPermutation {
645

746
// V0
@@ -163,4 +202,86 @@ public void nextPermutation_2(int[] nums) {
163202
return;
164203
}
165204

205+
// V3
206+
// IDEA : 2 POINTERS (gpt)
207+
/**
208+
* IDEA :
209+
*
210+
* Explanation of the Algorithm:
211+
*
212+
* 1. Identify the first decreasing element from the end:
213+
* • Traverse the array from the right and find the first index i such that nums[i] < nums[i + 1].
214+
* • This is the pivot point where the next permutation is possible.
215+
*
216+
* 2. Find the smallest element larger than nums[i] to the right of i:
217+
* • From the end of the array, find the first index j such that nums[j] > nums[i].
218+
*
219+
* 3. Swap nums[i] and nums[j]:
220+
* • Swap these elements to ensure the next permutation is lexicographically greater.
221+
*
222+
* 4. Reverse the suffix starting from i + 1:
223+
* • Reverse the elements to the right of i to ensure they are in ascending order, giving the smallest lexicographical order for the suffix.
224+
*
225+
* 5. If no i is found (i.e., the array is in descending order):
226+
* • Reverse the entire array to reset it to the smallest permutation.
227+
*
228+
*/
229+
public void nextPermutation_3(int[] nums) {
230+
int n = nums.length;
231+
/**
232+
* why set i = n - 2 ?
233+
*
234+
* The initialization of int i = n - 2; in the next permutation algorithm is essential for the logic of finding the first decreasing element when traversing the array from right to left. Here’s why it starts at n - 2:
235+
*
236+
* Understanding the Reason:
237+
*
238+
* 1. i Represents the Pivot Point:
239+
* • The algorithm tries to find the largest index i where nums[i] < nums[i + 1].
240+
* • This index i is called the pivot because it is where the current permutation can be adjusted to create the next lexicographically greater permutation.
241+
* 2. Start from the Second-to-Last Element (n - 2):
242+
* • The last element (nums[n - 1]) has no element to its right, so it cannot be compared with anything.
243+
* • To compare nums[i] with nums[i + 1], i must start from n - 2, the second-to-last index.
244+
* 3. Traverse from Right to Left:
245+
* • By moving from right to left, the algorithm ensures that we find the last occurrence of nums[i] < nums[i + 1], which is the correct pivot point for generating the next permutation.
246+
* • If no such i is found (i.e., the array is in descending order), the array is reversed to reset it to the smallest permutation.
247+
*
248+
*/
249+
int i = n - 2;
250+
251+
// Step 1: Find the first decreasing element from the end
252+
while (i >= 0 && nums[i] >= nums[i + 1]) {
253+
i--;
254+
}
255+
256+
if (i >= 0) { // If such an element is found
257+
// Step 2: Find the smallest element larger than nums[i]
258+
int j = n - 1;
259+
while (nums[j] <= nums[i]) {
260+
j--;
261+
}
262+
263+
// Step 3: Swap nums[i] and nums[j]
264+
swap_3(nums, i, j);
265+
}
266+
267+
// Step 4: Reverse the suffix starting at i + 1
268+
reverse_3(nums, i + 1, n - 1);
269+
}
270+
271+
// Helper method to swap two elements
272+
private void swap_3(int[] nums, int i, int j) {
273+
int temp = nums[i];
274+
nums[i] = nums[j];
275+
nums[j] = temp;
276+
}
277+
278+
// Helper method to reverse part of the array
279+
private void reverse_3(int[] nums, int start, int end) {
280+
while (start < end) {
281+
swap_3(nums, start, end);
282+
start++;
283+
end--;
284+
}
285+
}
286+
166287
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3522,6 +3522,33 @@ private TreeNode buildTree(List<Integer> input){
35223522
return null;
35233523
}
35243524

3525+
// LC 31
3526+
// https://leetcode.com/problems/next-permutation/description/
3527+
// 10.49 - 11.20 AM
3528+
/**
3529+
*
3530+
* exp 1) [1,2,3] -> [1,3,2]
3531+
*
3532+
*
3533+
* exp 2) [2,3,1] -> [3,1,2]
3534+
*
3535+
* Idea 1)
3536+
*
3537+
* from right -> left, find if there is a bigger val
3538+
* if yes, then find right most place (idx) and swap
3539+
* the bigger val with the value at idx
3540+
*
3541+
*
3542+
* find max val, mo
3543+
*
3544+
*
3545+
*
3546+
*
3547+
*/
3548+
public void nextPermutation(int[] nums) {
3549+
3550+
}
3551+
35253552
}
35263553

35273554

0 commit comments

Comments
 (0)