leetcode42接雨水问题

接雨水

题目描述

题目分析

核心思想:

代码

java版本:

java 复制代码
package com.pxx.leetcode.trapRainWaterDoublePoniter;

public class Solution1 {
    public int trap(int[] height) {
        if (height.length == 0) {
            return 0;
        }
        int n = height.length;
        int left = 0;
        int right = n - 1;

        int leftMax = height[0];
        int rightMax = height[n - 1];

        int ans = 0;//保留水通的总数的

        //边走边计算每一个位置
        while (left <= right) {
            leftMax = Math.max(leftMax, height[left]);
            rightMax = Math.max(rightMax, height[right]);

            //刚开始两个相等
            //我们的目的是找到一个最小值,然后减去当前的水通高度
            //数据分析:4 2 0 3 2 5
            //leftMax = height[0] = 4;
            //rightMax = height[5] = 5;
            //选择leftMax 因为它小,所以 ans = 4 - 4 = 0;//当前水通单位为0
            //既然leftMax被占据了之后,那么它就要++,因为这个桶的位置已经被计算完了left++
            //结束之后leftMax与rightMax已经别保留了下来,也就是leftMax = 4,rightMax = 5;
            //---------------------------进入循环(left(1) <= right(5))------------------------------------
            //判定左边最大值max(4(被上次保留的最大值),当前桶的高度=2)->leftmax = 4;
            //判定右边最大值max(5, 5)->rightMax = 5
            //这个时候是leftMax=4, 因为leftMax比较小,ans = 0(上面的水通单位,后面类推) + (4 - 2(当前水通高度) = 2)
            //上面就计算出当前水通能存储的水量是2 ans = 0+2;//目前占两个水通单位
            //保留最大值与最小值,杀入下一次循环里面,因为left又被占了,所以left又要++,right依旧保持不动
            //---------------------------进入循环(left(2) <= right(5))--------------------------------------
            //max(4, 0)->leftmax = 4
            //max(5, 5)->rightmax = 5
            //还是左边比较小,那么又杀入左边
            //ans = 2(上面的水桶数) + (4)(4-height[i(2)=0,4-0]) = 6;
            //--------------------------
            //改变情况简单分析
            //一旦左边leftMax > rightMax
            //就要从右边开始轮替占位了
            //核心思想:哪边小,哪边站位,哪边轮替
            if (leftMax < rightMax) {
                ans += leftMax - height[left];
                left++;
            } else {
                ans += rightMax - height[right];
                right--;
            }
        }
        return ans;
    }
}

c语言版本:

cpp 复制代码
#include <stdio.h>

int trap(int height[], int n) {
    if (n == 0) {
        return 0;
    }

    int left = 0;
    int right = n - 1;

    int leftMax = height[0];
    int rightMax = height[n - 1];

    int ans = 0;

    while (left <= right) {
        leftMax = leftMax > height[left] ? leftMax : height[left];
        rightMax = rightMax > height[right] ? rightMax : height[right];

        if (leftMax < rightMax) {
            ans += leftMax - height[left];
            left++;
        } else {
            ans += rightMax - height[right];
            right--;
        }
    }
    return ans;
}

int main() {
    // 示例用法
    int height[] = {4, 2, 0, 3, 2, 5};
    int n = sizeof(height) / sizeof(height[0]);

    int result = trap(height, n);

    printf("存储的水量是:%d\n", result);

    return 0;
}
相关推荐
程序员大雄学编程1 小时前
「深度学习笔记4」深度学习优化算法完全指南:从梯度下降到Adam的实战详解
笔记·深度学习·算法·机器学习
小O的算法实验室1 小时前
2022年ASOC SCI2区TOP,基于竞争与合作策略的金字塔粒子群算法PPSO,深度解析+性能实测,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
南莺莺2 小时前
邻接矩阵的基本操作
数据结构·算法··邻接矩阵
微波仿真2 小时前
实现多通道ADC多次测量取平均值,使用DMA
算法
余俊晖3 小时前
多模态文档理解视觉token剪枝思路
人工智能·算法·剪枝·多模态
aramae3 小时前
详细分析平衡树--红黑树(万字长文/图文详解)
开发语言·数据结构·c++·笔记·算法
再卷也是菜3 小时前
C++篇(13)计算器实现
c++·算法
CHEN5_023 小时前
【leetcode100】和为k的子数组(两种解法)
java·数据结构·算法
Codeking__3 小时前
DFS算法原理及其模板
算法·深度优先·图论
Victory_orsh4 小时前
“自然搞懂”深度学习系列(基于Pytorch架构)——01初入茅庐
人工智能·pytorch·python·深度学习·算法·机器学习