leetcode-11 Container With Most Water

本文探讨了两个经典的盛水问题,一是寻找两个垂直线形成的最大容器容量,二是计算降雨后地图上的蓄水量。通过分析,提供了高效算法解决方案,并附带详细代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.
问题:
对于给定的数组a1,a2,…,an(0<=ai,0<=i<=n),求解W的最大值。

W(i,j) = min(ai,aj)*(j-i)(0 <= i < j <= n)

最直观的办法,暴力方法,两层for循环

int maxValue = 0;
for(int i = 0; i < n; ++i)
{
    for(int j = i+1; j < n; ++j)
    {
        int dist = j - i;
        int depth = (a[i] > a[j] ? a[j] : a[i]);
        int tmpArea = dist * depth;
        if(tmpArea > maxValue)
        {
            maxValue = tmpArea;
        }       
    }
}

这个肯定解决不了问题,而且估计也违背了出题者的初衷,想想其他办法,其实影响W的只有两个因素,ai和aj的最小值,其次,i和j的距离。可以先考虑一种情况,如果存在k, j>k > i,而且ak <= ai,那么W(k,j) < W(i,j),如果ak<=aj,那么W(i,k)

int maxValue = 0;
int left = 0;
int right = n-1;
while(left < right)
{
    int depth = (a[left] > a[right] ? a[right] : a[left]);
    int tmpArea = (right - left) * depth;
    if(tmpArea > maxValue)
    {
        maxValue = tmpArea;
    }
    if(a[left] > a[right])
    {
        right = right - 1;
    }
    else
    {
        left = left + 1;
    }
}

至此,题目完美解决,再来看一个同类型的题目

42题,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.
这里写图片描述(图片来源leetcode网站)
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!

Example:

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

思考:
给定数组a(1),a(2),…,a(n),对于k是否能蓄水,而且能储蓄多少单位的水,取决于a(k)的左边是否存在i(0< i < k)以及j(n > j > k),如果满足:
min(a(i), a(j)) > a(k),则k单位的桶上面可以蓄水,否则不能蓄水。
可以定义:

    left(i) = max(a1, a2, ..., a(i-1))
     right(i) = max(a(i+1),a(i+2),...,a(n))

则对于k(0 < k < n)的蓄水量则等于:

    w(k)=max(min(left(k),right(k)) - a(k),0)

总体代码如下:

    class Solution {
public:
    int trap(vector<int>& height) {
        size_t nSize = height.size();
        if(nSize <= 2)
        {
            return 0;
        }
        int *left = new int [nSize + 1];
        memset(left, 0, sizeof(int) * (nSize + 1));
        int *right = new int [nSize + 1];
        memset(right, 0, sizeof(int) * (nSize + 1));

        for(int i = 0; i < nSize; ++i)
        {
            if(height[i] > left[i])
            {
                left[i + 1] = height[i];
            }
            else
            {
                left[i + 1] = left[i];
            }
            int j = nSize - i - 1;
            if(height[j] > right[j + 1])
            {
                right[j] = height[j];
            }
            else
            {
                right[j] = right[j + 1];
            }
        }
        int maxArea = 0;
        for(int i = 0; i < nSize; ++i)
        {
            int depth = (left[i + 1] > right[i + 1] ? right[i + 1] : left[i + 1]);
            if(height[i] < depth)
            {
                maxArea += depth - height[i];
            }
        }
        if(left != NULL)
        {
            delete []left;
            left = NULL;
        }
        if(right != NULL)
        {
            delete []right;
            right = NULL;
        }
        return maxArea;

    }
};
基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值