Skip to content

Commit

Permalink
update 31 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 22, 2024
1 parent 3e1d78a commit fa9f728
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 16 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20241122: 776
20241122: 776,31
20241121: 004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)
20241120: 986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
20241117: 394
Expand Down
30 changes: 16 additions & 14 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2025-01-16 -> ['776,31']
2025-01-15 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['394']
Expand All @@ -7,30 +8,31 @@
2025-01-02 -> ['1188,130,855(again)']
2024-12-28 -> ['900']
2024-12-27 -> ['26,27', '802,1197,26']
2024-12-26 -> ['776,31']
2024-12-25 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-21 -> ['394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['951,792']
2024-12-14 -> ['53,210,207', '163,1048']
2024-12-13 -> ['444', '298,729']
2024-12-13 -> ['776,31', '444', '298,729']
2024-12-12 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-08 -> ['394', '737']
2024-12-07 -> ['833,950', '900', '686,734,737']
2024-12-06 -> ['26,27', '802,1197,26', '353']
2024-12-05 -> ['528,334']
2024-12-05 -> ['776,31', '528,334']
2024-12-04 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2024-12-03 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-01 -> ['53,210,207']
2024-11-30 -> ['394', '444', '855,846', '1145,1219']
2024-11-30 -> ['776,31', '394', '444', '855,846', '1145,1219']
2024-11-29 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
2024-11-28 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-27 -> ['951,792', '524,221,889']
2024-11-27 -> ['776,31', '951,792', '524,221,889']
2024-11-26 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
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']
2024-11-24 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
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']
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']
2024-11-24 -> ['776,31', '004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
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']
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']
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']
2024-11-20 -> ['394', '939']
Expand Down Expand Up @@ -847,7 +849,7 @@
2020-07-23 -> ['388']
2020-07-22 -> ['269']
2020-07-21 -> ['646', '537,553,579']
2020-07-20 -> ['776', '210,261']
2020-07-20 -> ['210,261', '776']
2020-07-19 -> ['066,271', '361,393,133,207', '355,119,536']
2020-07-18 -> ['734,737', '771,775', '451,414,189']
2020-07-16 -> ['163', '762', '725,538,089']
Expand All @@ -859,26 +861,26 @@
2020-07-10 -> ['734,737', '388', '836,860,863']
2020-07-09 -> ['066,271', '694']
2020-07-08 -> ['066,271', '163', '646', '663']
2020-07-07 -> ['776', '066,271', '210,261', '298', '661,662', '703,787,819']
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
2020-07-02 -> ['210,261', '734,737', '298', '388', '742', '445,508']
2020-07-01 -> ['130', '269', '210,261', '361,393,133,207', '322,380,394']
2020-06-30 -> ['210,261', '361,393,133,207', '734,737', '346,686', '646', '738', '537,553,579', '289,295']
2020-06-29 -> ['776', '361,393,133,207', '734,737', '298', '396', '251,287']
2020-06-29 -> ['361,393,133,207', '734,737', '298', '776', '396', '251,287']
2020-06-28 -> ['130', '734,737', '482,127,102,107', '355,119,536', '231,232,240']
2020-06-27 -> ['298', '346,686', '388', '771,775', '451,414,189', '198,203,230']
2020-06-26 -> ['130', '298', '694', '155,165,167,179']
2020-06-25 -> ['130', '298', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
2020-06-24 -> ['776', '130', '346,686', '388', '742', '661,662', '517,535,529', '153']
2020-06-24 -> ['130', '346,686', '388', '776', '742', '661,662', '517,535,529', '153']
2020-06-23 -> ['346,686', '482,127,102,107', '711,046,126,127', '131,134,150']
2020-06-22 -> ['482,127,102,107', '388', '646', '771,775', '738', '701,450', '063,064,120,0146']
2020-06-21 -> ['776', '482,127,102,107', '388', '396', '640,645', '675,297,138']
2020-06-21 -> ['482,127,102,107', '388', '776', '396', '640,645', '675,297,138']
2020-06-20 -> ['388', '646', '762', '606,459']
2020-06-19 -> ['776', '646', '771,775', '742', '836,860,863']
2020-06-18 -> ['776', '269', '646', '694']
2020-06-19 -> ['646', '776', '771,775', '742', '836,860,863']
2020-06-18 -> ['269', '646', '776', '694']
2020-06-17 -> ['776', '771,775', '762', '738', '663', '537,553,579']
2020-06-16 -> ['771,775', '742', '396', '661,662', '703,787,819']
2020-06-15 -> ['771,775', '762', '355,119,536', '669,682,739,763']
Expand Down
123 changes: 122 additions & 1 deletion leetcode_java/src/main/java/LeetCodeJava/Array/NextPermutation.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
package LeetCodeJava.Array;

// https://leetcode.com/problems/next-permutation/description/

/**
* 31. Next Permutation
* Solved
* Medium
* Topics
* Companies
* A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
*
* 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].
* 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).
*
* For example, the next permutation of arr = [1,2,3] is [1,3,2].
* Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
* While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
* Given an array of integers nums, find the next permutation of nums.
*
* The replacement must be in place and use only constant extra memory.
*
*
*
* Example 1:
*
* Input: nums = [1,2,3]
* Output: [1,3,2]
* Example 2:
*
* Input: nums = [3,2,1]
* Output: [1,2,3]
* Example 3:
*
* Input: nums = [1,1,5]
* Output: [1,5,1]
*
*
* Constraints:
*
* 1 <= nums.length <= 100
* 0 <= nums[i] <= 100
*
*/
public class NextPermutation {

// V0
Expand Down Expand Up @@ -163,4 +202,86 @@ public void nextPermutation_2(int[] nums) {
return;
}

// V3
// IDEA : 2 POINTERS (gpt)
/**
* IDEA :
*
* Explanation of the Algorithm:
*
* 1. Identify the first decreasing element from the end:
* • Traverse the array from the right and find the first index i such that nums[i] < nums[i + 1].
* • This is the pivot point where the next permutation is possible.
*
* 2. Find the smallest element larger than nums[i] to the right of i:
* • From the end of the array, find the first index j such that nums[j] > nums[i].
*
* 3. Swap nums[i] and nums[j]:
* • Swap these elements to ensure the next permutation is lexicographically greater.
*
* 4. Reverse the suffix starting from i + 1:
* • Reverse the elements to the right of i to ensure they are in ascending order, giving the smallest lexicographical order for the suffix.
*
* 5. If no i is found (i.e., the array is in descending order):
* • Reverse the entire array to reset it to the smallest permutation.
*
*/
public void nextPermutation_3(int[] nums) {
int n = nums.length;
/**
* why set i = n - 2 ?
*
* 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:
*
* Understanding the Reason:
*
* 1. i Represents the Pivot Point:
* • The algorithm tries to find the largest index i where nums[i] < nums[i + 1].
* • This index i is called the pivot because it is where the current permutation can be adjusted to create the next lexicographically greater permutation.
* 2. Start from the Second-to-Last Element (n - 2):
* • The last element (nums[n - 1]) has no element to its right, so it cannot be compared with anything.
* • To compare nums[i] with nums[i + 1], i must start from n - 2, the second-to-last index.
* 3. Traverse from Right to Left:
* • 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.
* • 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.
*
*/
int i = n - 2;

// Step 1: Find the first decreasing element from the end
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}

if (i >= 0) { // If such an element is found
// Step 2: Find the smallest element larger than nums[i]
int j = n - 1;
while (nums[j] <= nums[i]) {
j--;
}

// Step 3: Swap nums[i] and nums[j]
swap_3(nums, i, j);
}

// Step 4: Reverse the suffix starting at i + 1
reverse_3(nums, i + 1, n - 1);
}

// Helper method to swap two elements
private void swap_3(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

// Helper method to reverse part of the array
private void reverse_3(int[] nums, int start, int end) {
while (start < end) {
swap_3(nums, start, end);
start++;
end--;
}
}

}
27 changes: 27 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3522,6 +3522,33 @@ private TreeNode buildTree(List<Integer> input){
return null;
}

// LC 31
// https://leetcode.com/problems/next-permutation/description/
// 10.49 - 11.20 AM
/**
*
* exp 1) [1,2,3] -> [1,3,2]
*
*
* exp 2) [2,3,1] -> [3,1,2]
*
* Idea 1)
*
* from right -> left, find if there is a bigger val
* if yes, then find right most place (idx) and swap
* the bigger val with the value at idx
*
*
* find max val, mo
*
*
*
*
*/
public void nextPermutation(int[] nums) {

}

}


0 comments on commit fa9f728

Please sign in to comment.