Skip to content

Latest commit

 

History

History
87 lines (62 loc) · 1.77 KB

20210427.md

File metadata and controls

87 lines (62 loc) · 1.77 KB

Algorithm

41. First Missing Positive

Description

Given an unsorted integer array nums, find the smallest missing positive integer.

Example 1:

Input: nums = [1,2,0]
Output: 3

Example 2:

Input: nums = [3,4,-1,1]
Output: 2

Example 3:

Input: nums = [7,8,9,11,12]
Output: 1

Constraints:

  • 1 <= nums.length <= 300

  • -231 <= nums[i] <= 231 - 1

  • Follow up: Could you implement an algorithm that runs in O(n) time and uses constant extra space?

Solution

public class Solution {
public int firstMissingPositive(int[] nums) {
    int n = nums.length;

    // 1. mark numbers (num < 0) and (num > n) with a special marker number (n+1)
    // (we can ignore those because if all number are > n then we'll simply return 1)
    for (int i = 0; i < n; i++) {
        if (nums[i] <= 0 || nums[i] > n) {
            nums[i] = n + 1;
        }
    }
    // note: all number in the array are now positive, and on the range 1..n+1

    // 2. mark each cell appearing in the array, by converting the index for that number to negative
    for (int i = 0; i < n; i++) {
        int num = Math.abs(nums[i]);
        if (num > n) {
            continue;
        }
        num--; // -1 for zero index based array (so the number 1 will be at pos 0)
        if (nums[num] > 0) { // prevents double negative operations
            nums[num] = -1 * nums[num];
        }
    }

    // 3. find the first cell which isn't negative (doesn't appear in the array)
    for (int i = 0; i < n; i++) {
        if (nums[i] >= 0) {
            return i + 1;
        }
    }

    // 4. no positive numbers were found, which means the array contains all numbers 1..n
    return n + 1;
}
}

Discuss

Review

Tip

Share