leetcode Container With Most Water
LeetCode with container most Water
2023-09-11 14:20:12 时间
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.
This problem is different from largest rectangle in histogram. It's just a line instead of a histogram. So there is no water.
class Solution { public: int maxArea(vector<int> &height) { int size = height.size(), l = 0, r = size - 1, res = 0; if (size == 0) return 0; while (l < r) { if (res < (r - l)*min(height[l],height[r])) res = (r - l)*min(height[l],height[r]); if (height[l] <= height[r]) ++l; else --r; } return res; } };
相关文章
- Leetcode: Longest Substring with At Least K Repeating Characters
- Leetcode: Longest Substring with At Most Two Distinct Characters
- JS leetcode 杨辉三角 超详细题解分析
- 基于人脸识别的考勤系统 — Flask App — With GUI — with source code
- [LeetCode]面试题10- II. 青蛙跳台阶问题
- [LeetCode]Container With Most Water, 解题报告
- 【LeetCode】210. Course Schedule II
- leetcode - Sort Colors
- LeetCode 36 Sudoku Solver
- [LeetCode] 1277. Count Square Submatrices with All Ones 统计全为 1 的正方形子矩阵
- [LeetCode] 1013. Partition Array Into Three Parts With Equal Sum 将数组分成和相等的三个部分
- [LeetCode] 930. Binary Subarrays With Sum 二元子数组之和
- [LeetCode] 860. Lemonade Change 买柠檬找零
- [LeetCode] Average of Levels in Binary Tree 二叉树的层平均值
- [LeetCode] 373. Find K Pairs with Smallest Sums 找和最小的K对数字
- [LeetCode] Integer Replacement 整数替换
- [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
- [LeetCode] 131. Palindrome Partitioning 拆分回文串
- Leetcode——11. Container With Most Water