@@ -3524,7 +3524,7 @@ private TreeNode buildTree(List<Integer> input){
35243524
35253525 // LC 31
35263526 // https://leetcode.com/problems/next-permutation/description/
3527- // 10.49 - 11.20 AM
3527+ // 5.39 pm - 6.15 pm
35283528 /**
35293529 *
35303530 * exp 1) [1,2,3] -> [1,3,2]
@@ -3541,9 +3541,36 @@ private TreeNode buildTree(List<Integer> input){
35413541 *
35423542 * find max val, mo
35433543 *
3544+ * ================================
35443545 *
3546+ * Idea 2)
35453547 *
3548+ * 1) from right -> left, find 1st "pivot"
3549+ * -> if there is a val bigger than its left
3550+ * (if no pivot, means input array already sorted, return directly
35463551 *
3552+ *
3553+ * 2) from left -> right, find 1st element that smaller than pivot,
3554+ * if found, swap (pivot <---> element)
3555+ *
3556+ * 3) reorder (small -> big ?? ) all val on the right-hand side of element
3557+ *
3558+ *
3559+ *
3560+ * demo
3561+ *
3562+ * input : [1,2,3]
3563+ *
3564+ * -> [1,2,3] , so pivot is 3
3565+ * i
3566+ *
3567+ * -> [1,2,3] 1 < 3, cur = 3
3568+ * j
3569+ *
3570+ * -> [1,2,3] 2 < 3, cur = 2
3571+ * i
3572+ *
3573+ * -> so, swap 2 and 3 -> [1,3,2]
35473574 */
35483575 public void nextPermutation (int [] nums ) {
35493576
0 commit comments