First Missing Positive

Problem

Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.

You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

Example 1:

Input: nums = [1,2,0]
Output: 3
Explanation: The numbers in the range [1,2] are all in the array.

Example 2:

Input: nums = [3,4,-1,1]
Output: 2
Explanation: 1 is in the array but 2 is missing.

Example 3:

Input: nums = [7,8,9,11,12]
Output: 1
Explanation: The smallest positive integer 1 is missing.

Constraints:

  • 1 <= nums.length <= 105
  • -231 <= nums[i] <= 231 - 1

Solution

Another Cyclic Sort

class Solution {
    public int firstMissingPositive(int[] nums) {
        var i = 0;
        while (i < nums.length) {
            if (nums[i] > 0 && nums[i] <= nums.length && nums[i] != nums[nums[i] - 1]) {
                swap(nums, i, nums[i] - 1);
            } else {
                i += 1;
            }
        }

        for (i = 0; i < nums.length; i++) {
            if (nums[i] != i + 1) {
                return i + 1;
            }
        }

        return nums.length + 1;
    }

    void swap(int[] nums, int l, int r) {
        nums[l] ^= nums[r];
        nums[r] ^= nums[l];
        nums[l] ^= nums[r];
    }
}

Cyclic Sort

class Solution {
    public int firstMissingPositive(int[] nums) {
        // cyclic sort
        var i = 0;
        while (i < nums.length) {
            var dst = nums[i] - 1;
            if (nums[i] > 0 && nums[i] <= nums.length && nums[i] != nums[dst]) {
                swap(nums, i, dst);
            } else {
                i += 1;
            }
        }

        for (i = 0; i < nums.length; i++) {
            if (nums[i] != i + 1) {
                return i + 1;
            }
        }

        return nums.length + 1;
    }

    void swap(int[] nums, int x, int y) {
        var tmp = nums[x];
        nums[x] = nums[y];
        nums[y] = tmp;
    }
}

XOR

This didn’t work because the array allows duplicate numbers :/

class Solution {
    public int firstMissingPositive(int[] nums) {
        var x = 0;
        var min = Integer.MAX_VALUE;
        var max = Integer.MIN_VALUE;
        for (var n : nums) {
            if (n < 1) {
                continue;
            }
            min = Math.min(min, n);
            max = Math.max(max, n);
            x ^= n;
        }
        if (min > 1) {
            return 1;
        }
        for (var i = min; i <= max; i++) {
            x ^= i;
        }
        if (x == 0) {
            return max + 1;
        }
        return x;
    }
}

Recent posts from blogs that I like

Sequoia introduces pinning to iCloud Drive

If you have Optimise Mac Storage enabled for iCloud Drive, this new feature lets you pin the files you want to be stored locally and not evicted. Full details.

via The Eclectic Light Company

Notes on running Go in the browser with WebAssembly

Recently I've had to compile Go to WebAssembly to run in the browser in a couple of small projects (#1, #2), and in general spent some time looking at WebAssembly. I find WebAssembly to be an exciting technology, both for the web and for other uses (e.g. with WASI); specifically, it's pretty great t...

via Eli Bendersky

I fixed the strawberry problem because OpenAI couldn't

Remember kids: real winners cheat

via Xe Iaso