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, height[i]).

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 <= height[i] <= 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;
    }
};
相关推荐
QxQ么么35 分钟前
移远通信(桂林)26校招-助理AI算法工程师-面试纪录
人工智能·python·算法·面试
止观止3 小时前
C++20 Concepts:让模板错误信息不再“天书”
c++·c++20·编程技巧·模板编程·concepts
FL16238631293 小时前
ONNX RuntimeC++ 静态库下载安装和使用教程
开发语言·c++
Mz12213 小时前
day05 移动零、盛水最多的容器、三数之和
数据结构·算法·leetcode
SoleMotive.3 小时前
如果用户反映页面跳转得非常慢,该如何排查
jvm·数据库·redis·算法·缓存
誰能久伴不乏3 小时前
Linux文件套接字AF_UNIX
linux·服务器·c语言·c++·unix
念越3 小时前
判断两棵二叉树是否相同(力扣)
算法·leetcode·入门
豆豆plus3 小时前
C++实现文件操作类
开发语言·c++
complexor3 小时前
NOIP 2025 游记
数据结构·数学·动态规划·贪心·组合计数·树上问题·游记&总结