刷题笔记:对撞型/相会型指针(1) 灌水类

1 Trapping Rain Water

Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.

For example,
Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6.

The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.Thanks Marcos for contributing this image!

? Solve this problem

[解题思路]
对于任何一个坐标,检查其左右的最大坐标,然后相减就是容积。所以,
1. 从左往右扫描一遍,对于每一个坐标,求取左边最大值。
2. 从右往左扫描一遍,对于每一个坐标,求最大右值。
3. 再扫描一遍,求取容积并加和。
#2和#3可以合并成一个循环,

扫描3遍数组的方法:

public class Solution {
    public int trap(int[] height) {
        if (height == null || height.length == 0) {
            return 0;
        }
        int len = height.length;
        int res = 0;
        int[] leftMax = new int[len];
        int[] rightMax = new int[len];
        leftMax[0] = height[0];
        rightMax[len - 1] = height[len - 1];
        for (int i = 1; i < len; i++) {
            leftMax[i] = Math.max(leftMax[i - 1], height[i]);
        }
        for (int i = len - 2; i >= 0; i--) {
            rightMax[i] = Math.max(rightMax[i + 1], height[i]);
        }
        for (int i = 1; i < len - 1; i++) {
            int amount = Math.min(leftMax[i - 1], rightMax[i + 1]) - height[i];
            if (amount > 0) {
                res += amount;
            }
        }
        return res;
    }
}

可以简化为对撞型指针,扫描1遍数组,总体思想类似

其实可以看作是一个动态规划的滚动数组优化,把leftMax, rightMax优化为o(1)变量

public class Solution {
    public int trap(int[] height) {
        if (height == null || height.length < 3) {
            return 0;
        }
        int len = height.length;
        int leftHeight = height[0];
        int rightHeight = height[len - 1];
        int left = 0;
        int right = len - 1;
        int res = 0;

        while (left < right) {
            if (height[left] < height[right]) {
                if (leftHeight > height[left]) {
                    res += leftHeight - height[left];
                } else {
                    leftHeight = height[left];
                }
                left++;
            } else {
                if (rightHeight > height[right]) {
                    res+= rightHeight - height[right];
                } else {
                    rightHeight = height[right];
                }
                right--;
            }
        } // end of left < right
        return res;
    }
}

trap

2. //todo

时间: 2024-10-05 12:35:13

刷题笔记:对撞型/相会型指针(1) 灌水类的相关文章

【leetcode刷题笔记】Sqrt(x)

Implement int sqrt(int x). Compute and return the square root of x. 题解:二分的方法,从0,1,2.....x搜索sqrt(x)的值. 代码如下: 1 public class Solution { 2 public int sqrt(int x) { 3 long l = 0; 4 long r = x; 5 6 while(l <= r){ 7 long mid = l + (r-l)/2; 8 if(x == mid*mi

【leetcode刷题笔记】Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 题解:要求不用乘除和取模运算实现两个数的除法. 那么用加减法是很自然的选择.不过如果一次只从被除数中剪掉一个除数会TLE.所以我们借助移位运算,依次从被除数中减去1个除数,2个除数,4个除数......当减不动的时候,再依次从被除数中减去......4个除数,2个除数,1个除数. 例如50除以5的计算过程如下: dividend exp tem

【leetcode刷题笔记】Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A = 

【leetcode刷题笔记】Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

【leetcode刷题笔记】Sort List

Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找到中点并返回的函数findMiddle; 2.归并函数merge; 3.排序函数sortList. 数组的findMiddle函数非常容易实现,链表就有一点tricky了.首先设置两个指针,一个slow初始化为head,一个fast初始化为head.next,然后slow一次走一步,fast一次走两

【leetcode刷题笔记】Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

【leetcode刷题笔记】Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->3, return 2->3.

【leetcode刷题笔记】ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line:  "PAHNAPLSI

【leetcode刷题笔记】Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the next node of a

【leetcode刷题笔记】Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n