LeetCode75——Day12

文章目录

一、题目

11. Container With Most Water

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, heighti).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

Example 1:

Input: height = 1,8,6,2,5,4,8,3,7

Output: 49

Explanation: The above vertical lines are represented by array 1,8,6,2,5,4,8,3,7. In this case, the max area of water (blue section) the container can contain is 49.

Example 2:

Input: height = 1,1

Output: 1

Constraints:

n == height.length

2 <= n <= 105

0 <= heighti <= 104

二、题解

双指针+贪心

cpp 复制代码
class Solution {
public:
    int maxArea(vector<int>& height) {
        int n = height.size();
        int left = 0,right = n - 1;
        int maxS = 0;
        while(left < right){
            int h1 = height[left];
            int h2 = height[right];
            int S = min(h1,h2) * (right - left);
            maxS = max(maxS,S);
            if(h1 > h2) right--;
            else left++;
        }
        return maxS;
    }
};
相关推荐
十五年专注C++开发8 分钟前
qobject_cast转换失败原因分析
c++·qt·dynamic_cast·qobject_cast
小保CPP20 分钟前
OCR C++ Tesseract按单词识别字符
c++·人工智能·ocr·模式识别·光学字符识别
库克克23 分钟前
【C++】多态
开发语言·c++
Shadow(⊙o⊙)28 分钟前
日志与线程池
linux·c++·算法
冷小鱼34 分钟前
AI Agent 的核心算法:多智能体协作(Multi-Agent Systems)
前端·人工智能·算法·multi-agent·多智能体协作·systems
QN1幻化引擎40 分钟前
# DalinX V8 灵鉴 V2:12维意识评测框架 —— 从 Tononi IIT 到 Friston FEP 的理论统一
数据库·人工智能·算法·机器学习·数据挖掘·agi
牧以南歌〆44 分钟前
数据结构<一>顺序表
c语言·数据结构·算法
我不是懒洋洋1 小时前
从零实现一个分布式限流器:令牌桶与漏桶
c++
Fairy要carry1 小时前
面试-训练显存占用估计
算法
触底反弹1 小时前
💥 暴力解 LeetCode 导致 OOM:10000 行数据撑爆 V8 堆内存的完整排查
javascript·算法·面试