package com.fishercoder.solutions;
/**
 * 31. Next Permutation

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1*/
public class _31 {
    /**Leetcode has a very good article to illustrate this problem and with animation: https://leetcode.com/articles/next-permutation/
     * 1. if the array is already in decrementing order, then there's no next larger permutation possible. 
     * 2. if not, start from the end of the array, find the first pair of numbers that break the decrementing order
     * 3. then from that index going to the right again, find the element that is closest bigger than this number, swap them
     * 4. reverse the right half of this array after this index*/

    public void nextPermutation(int[] nums) {
        int i = nums.length - 2;
        while (i >= 0 && nums[i] >= nums[i + 1]) {
            i--;
        }
        if (i >= 0) {
            int j = nums.length - 1;
            while (j >= 0 && nums[i] >= nums[j]) {
                j--;
            }

            swap(nums, i, j);
        }

        reverse(nums, i + 1);
    }

    private void reverse(int[] nums, int start) {
        int end = nums.length - 1;
        while (start <= end) {
            int tmp = nums[start];
            nums[start++] = nums[end];
            nums[end--] = tmp;
        }
    }

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

}

@@ -28,16 +28,17 @@
 utions;%0A
+%0A
 /**%0A * 3
@@ -522,19 +522,22 @@
  %E2%86%92 1,5,1
+%0A 
 */%0A
+%0A
 public c
@@ -553,13 +553,55 @@
 %7B%0A  
-  /**
+public static class Solution1 %7B%0A    /**%0A     * 
 Leet
@@ -675,16 +675,23 @@
 imation:
+%0A     *
  https:/
@@ -837,17 +837,16 @@
 ossible.
- 
 %0A     * 
@@ -1142,16 +1142,22 @@
 is index
+%0A     
 */%0A%0A    
@@ -1194,26 +1194,24 @@
 t%5B%5D nums) %7B%0A
-  
       int i 
@@ -1231,26 +1231,24 @@
  - 2;%0A      
-  
 while (i %3E= 
@@ -1290,19 +1290,13 @@
     
-    
 i--;%0A
-  
     
@@ -1305,18 +1305,16 @@
 %7D%0A      
-  
 if (i %3E=
@@ -1315,28 +1315,24 @@
  (i %3E= 0) %7B%0A
-    
         int 
@@ -1356,28 +1356,24 @@
  1;%0A        
- 
-   
 while (j %3E= 
@@ -1409,22 +1409,16 @@
         
-      
 j--;%0A   
@@ -1418,28 +1418,24 @@
 --;%0A        
-    
 %7D%0A%0A         
@@ -1433,20 +1433,16 @@
         
-  
-  
 swap(num
@@ -1453,31 +1453,27 @@
 , j);%0A      
-  
 %7D%0A%0A
-  
       revers
@@ -1541,26 +1541,24 @@
 nt start) %7B%0A
-  
       int en
@@ -1580,26 +1580,24 @@
  - 1;%0A      
-  
 while (start
@@ -1603,28 +1603,24 @@
 t %3C= end) %7B%0A
-    
         int 
@@ -1642,28 +1642,24 @@
 t%5D;%0A        
- 
-   
 nums%5Bstart++
@@ -1677,28 +1677,24 @@
 d%5D;%0A        
-    
 nums%5Bend--%5D 
@@ -1702,26 +1702,24 @@
  tmp;%0A      
- 
- 
 %7D%0A    %7D%0A%0A   
@@ -1763,34 +1763,32 @@
  int j) %7B%0A      
-  
 int tmp = nums%5Bi
@@ -1790,18 +1790,16 @@
 ums%5Bi%5D;%0A
-  
       nu
@@ -1815,18 +1815,16 @@
 ums%5Bj%5D;%0A
-  
       nu
@@ -1842,11 +1842,14 @@
 ;%0A    %7D%0A
+  %7D
 %0A%7D%0A

refactor 31