|
| 1 | +package arrays.hard; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/*** |
| 6 | + * Problem 41 in Leetcode: https://leetcode.com/problems/first-missing-positive/ |
| 7 | + * |
| 8 | + * Given an unsorted integer array nums, return the smallest missing positive integer. |
| 9 | + * You must implement an algorithm that runs in O(n) time and uses constant extra space. |
| 10 | + * |
| 11 | + * Example 1: |
| 12 | + * Input: nums = [1,2,0] |
| 13 | + * Output: 3 |
| 14 | + * |
| 15 | + * Example 2: |
| 16 | + * Input: nums = [3,4,-1,1] |
| 17 | + * Output: 2 |
| 18 | + * |
| 19 | + * Example 3: |
| 20 | + * Input: nums = [7,8,9,11,12] |
| 21 | + * Output: 1 |
| 22 | + */ |
| 23 | + |
| 24 | +public class FirstMissingPositive { |
| 25 | + public static void main(String[] args) { |
| 26 | +// int[] nums = {7, 8, 9, 11, 12}; |
| 27 | + int[] nums = {1, 2, 3, 4, 5}; |
| 28 | + |
| 29 | + System.out.println("Brute Force: " + firstMissingPositiveBruteForce(nums)); |
| 30 | + System.out.println("Sorting: " + firstMissingPositiveSorting(nums)); |
| 31 | + System.out.println("Count Array: " + firstMissingPositiveCountArray(nums)); |
| 32 | + System.out.println("Swapping: " + firstMissingPositiveSwapping(nums)); |
| 33 | + System.out.println("Negation: " + firstMissingPositiveNegation(nums)); |
| 34 | + } |
| 35 | + |
| 36 | + private static int firstMissingPositiveBruteForce(int[] nums) { |
| 37 | + int n = nums.length; |
| 38 | + |
| 39 | + for (int i = 1; i <= n; i++) { |
| 40 | + boolean found = false; |
| 41 | + for (int num : nums) { |
| 42 | + if (num == i) { |
| 43 | + found = true; |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + if (!found) { |
| 48 | + return i; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + return n + 1; |
| 53 | + } |
| 54 | + |
| 55 | + private static int firstMissingPositiveSorting(int[] nums) { |
| 56 | + Arrays.sort(nums); |
| 57 | + |
| 58 | + int missingNumber = 1; |
| 59 | + |
| 60 | + for (int num : nums) { |
| 61 | + if (num == missingNumber) { |
| 62 | + missingNumber++; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return missingNumber; |
| 67 | + } |
| 68 | + |
| 69 | + private static int firstMissingPositiveCountArray(int[] nums) { |
| 70 | + int n = (int) 1e5 + 2; |
| 71 | + int[] count = new int[n]; |
| 72 | + |
| 73 | + for (int num : nums) { |
| 74 | + if (num > 0) { |
| 75 | + count[num] = 1; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + for (int i = 1; i <= nums.length; i++) { |
| 80 | + if (count[i] == 0) { |
| 81 | + return i; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return nums.length + 1; |
| 86 | + } |
| 87 | + |
| 88 | + private static int firstMissingPositiveSwapping(int[] nums) { |
| 89 | + int n = nums.length; |
| 90 | + |
| 91 | + for (int i = 0; i < n; i++) { |
| 92 | + int position = nums[i] - 1; |
| 93 | + while (((position >= 0) && (position < n)) && (nums[i] != nums[position])) { |
| 94 | + int temp = nums[i]; |
| 95 | + nums[i] = nums[position]; |
| 96 | + nums[position] = temp; |
| 97 | + position = nums[i] - 1; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + for (int i = 0; i < n; i++) { |
| 102 | + if (i != (nums[i] - 1)) { |
| 103 | + return i + 1; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return n + 1; |
| 108 | + } |
| 109 | + |
| 110 | + private static int firstMissingPositiveNegation(int[] nums) { |
| 111 | + int n = nums.length; |
| 112 | + |
| 113 | + for (int i = 0; i < n; i++) { |
| 114 | + if (nums[i] <= 0) { |
| 115 | + nums[i] = 0; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + for (int num : nums) { |
| 120 | + num = Math.abs(num); |
| 121 | + int index = num - 1; |
| 122 | + if ((index >= 0) && (index < n)) { |
| 123 | + if (nums[index] == 0) { |
| 124 | + nums[index] = -(n + 1); |
| 125 | + } else if (nums[index] > 0) { |
| 126 | + nums[index] = -nums[index]; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + for (int i = 1; i <= n; i++) { |
| 132 | + int index = i - 1; |
| 133 | + if (nums[index] >= 0) { |
| 134 | + return i; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return n + 1; |
| 139 | + } |
| 140 | +} |
0 commit comments