LeetCode - the missing first positive number (41) and catch the rain (42)

Table of contents

missing first positive number

catch rainwater

0ms, 100% code


missing first positive number

Source: LeetCode
Link: https://leetcode.cn/problems/first-missing-positive


Question: Given an unsorted integer array nums, please find the smallest positive integer that does not appear in it.
Please implement a solution that has O(n) time complexity and uses only a constant level of extra space.

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

hint:

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

Ideas:

(1) Map a relationship to the value stored in the array a[i] i+1, so when a[i] = x, the subscript of x is x-1

(2) 0 -> a.length cycle, put a[i] in the position of a[i] - 1, if a[a[i] - 1] != a[i], adjust the positions of the two numbers

(3) The loop judges the missing number. If a[i] != i + 1, the missing number is i+1. If it is not found after the loop, it will return a.length+1

class Solution {
    public int firstMissingPositive(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] > 0 && nums[i] <= nums.length && nums[nums[i] - 1] != nums[i]) {
                //当 nums[i]大于零 且 nums[i]小于nums的长度 且 nums[nums[i - 1]]不等于nums[i] 的时候循环
                //nums[nums[i - 1]]和nums[i]进行交换
                int temp = nums[nums[i] - 1];
                nums[nums[i] - 1] = nums[i];
                nums[i] = temp;
            }
        }

        //判断缺失的数
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != i + 1) return i + 1;
        }
        return nums.length + 1;
    }
}


catch rainwater

Source: LeetCode
Link: https://leetcode.cn/problems/trapping-rain-water
 

Question: Given n non-negative integers representing the height map of each column with a width of 1, calculate how much rainwater can be received by the columns arranged in this way after it rains.

Example 1:

Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6


Explanation: The above is a height map represented by the array [0,1,0,2,1,0,1,3,2,1,2,1], in this case, it can receive 6 units of rainwater ( The blue part represents rainwater).

 

Example 2:

Input: height = [4,2,0,3,2,5]
Output: 9

hint:

    n == height.length
    1 <= n <= 2 * 104
    0 <= height[i] <= 105

Ideas:

(1) You need to find the first column greater than 0 as the left side of the U shape

(2) Find the pillar on the right after the left side is fixed. There are two possibilities for the pillar on the right

1) The first pillar found on the right >= the pillar on the left

2) The first column found on the right < the column on the left, but it cannot be 0

(3) Find the gap between the left column and the right column. Here I use an exhaustive method to try all possibilities

class Solution {
    public int trap(int[] height) {
        int ans = 0;//存放接水的数量
        int left = 0;//存放左边柱子的高度,默认为0
        for(int i = 0; i < height.length; i++) {
            if(height[i] >= 1) {//左侧的柱子必须大于等于一才可以接水
                left = i++;//左侧柱子高度等于i
                int now = 0;
                while(i < height.length && height[i] < height[left]) {//右边小于左边才可以存储
                    now += height[left] - height[i++];//左边减去右边
                }
                if(i < height.length) {
                    ans += now;
                    i--;//for后会有i++,所以要--来抵消++,右边的柱子可以当做下一个U型的左侧柱子
                } else {//右边柱子都比左边的低
                    i = left - 1;//回到左边的左边,i++抵消,重新回到left
                    height[left]--;//每次减去1去匹配右边更低的柱子
                }
            }
        }
        return ans;
    }
}

0ms, 100% code

class Solution {
    public int trap(int[] height) {
        int left = 0, right = height.length - 1;
        int maxL = height[left], maxR = height[right];
        int res = 0;
        while (left < right) {
            maxL = Math.max(maxL, height[left]);
            maxR = Math.max(maxR, height[right]);
            res += maxR > maxL ? maxL - height[left++] : maxR - height[right--];
        }
        return res;
    }
}

 

 

Guess you like

Origin blog.csdn.net/weixin_71646897/article/details/129658575