package leetcode;

import java.util.Arrays;

/**
 * https://leetcode.com/problems/first-missing-positive/
 */
public class Problem41 {
    public int firstMissingPositive(int[] nums) {
        if (nums.length == 0) {
            return 1;
        }
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] >= 1 && nums[i] < nums.length) {
                swap(nums, nums[nums[i]] - 1, nums[i]);
            }
        }
        System.out.println(Arrays.toString(nums));
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != i + 1) {
                return i + 1;
            }
        }
        return nums[nums.length - 1] + 1;
    }

    private static void swap(int[] nums, int i, int j) {
        if (i < 0 || i >= nums.length || j < 0 || j >= nums.length) {
            return;
        }
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    public static void main(String[] args) {
        Problem41 prob = new Problem41();
//        System.out.println(prob.firstMissingPositive(new int[]{1, 2, 0})); // 3
//        System.out.println(prob.firstMissingPositive(new int[]{})); // 1
//        System.out.println(prob.firstMissingPositive(new int[]{0})); // 1
//        System.out.println(prob.firstMissingPositive(new int[]{2})); // 1
//        System.out.println(prob.firstMissingPositive(new int[]{1})); // 2
//        System.out.println(prob.firstMissingPositive(new int[]{1, 1})); // 2
//        System.out.println(prob.firstMissingPositive(new int[]{2, 2, 4})); // 1
        System.out.println(prob.firstMissingPositive(new int[]{0, 2, 2, 1, 1})); // 3
    }
}

@@ -16,34 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A
 /**%0A
@@ -165,52 +165,51 @@
    i
-f (nums.length == 0) %7B%0A            return 1;
+nt i = 0;%0A        while (i %3C nums.length) %7B
 %0A   
@@ -217,57 +217,98 @@
     
-%7D%0A
     
-    for (int i = 0; i %3C nums.length;
+if (nums%5Bi%5D == i + 1 %7C%7C nums%5Bi%5D %3C= 0 %7C%7C nums%5Bi%5D %3E= nums.length) %7B%0A               
  i++
-) %7B
+;
 %0A   
@@ -308,32 +308,39 @@
 ++;%0A            
+%7D else 
 if (nums%5Bi%5D %3E= 1
@@ -336,40 +336,31 @@
 ums%5B
-i%5D %3E= 1 && nums%5Bi%5D %3C nums.length
+nums%5Bi%5D - 1%5D != nums%5Bi%5D
 ) %7B%0A
@@ -390,110 +390,104 @@
 ms, 
-nums%5B
+i, 
 nums%5Bi%5D
-%5D
  - 1
-, nums%5Bi%5D);%0A            %7D%0A        %7D%0A        System.out.println(Arrays.toString(nums))
+);%0A            %7D else %7B%0A                i++;%0A            %7D%0A        %7D%0A        i = 0
 ;%0A  
@@ -497,25 +497,16 @@
    for (
-int i = 0
 ; i %3C nu
@@ -632,29 +632,9 @@
 urn 
-nums%5Bnums.length - 1%5D
+i
  + 1
@@ -885,733 +885,7 @@
    %7D
-%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem41 prob = new Problem41();%0A//        System.out.println(prob.firstMissingPositive(new int%5B%5D%7B1, 2, 0%7D)); // 3%0A//        System.out.println(prob.firstMissingPositive(new int%5B%5D%7B%7D)); // 1%0A//        System.out.println(prob.firstMissingPositive(new int%5B%5D%7B0%7D)); // 1%0A//        System.out.println(prob.firstMissingPositive(new int%5B%5D%7B2%7D)); // 1%0A//        System.out.println(prob.firstMissingPositive(new int%5B%5D%7B1%7D)); // 2%0A//        System.out.println(prob.firstMissingPositive(new int%5B%5D%7B1, 1%7D)); // 2%0A//        System.out.println(prob.firstMissingPositive(new int%5B%5D%7B2, 2, 4%7D)); // 1%0A        System.out.println(prob.firstMissingPositive(new int%5B%5D%7B0, 2, 2, 1, 1%7D)); // 3%0A    %7D
 %0A%7D%0A

Solve problem 41