
g0001_0100.s0011_container_with_most_water.Solution.cpp Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Greedy #Two_Pointers
// #Algorithm_II_Day_4_Two_Pointers #Big_O_Time_O(n)_Space_O(1)
// #2024_05_22_Time_54_ms_(88.17%)_Space_61.5_MB_(24.44%)
#include
using namespace std;
class Solution {
public:
int maxArea(vector& height) {
int maxArea = 0;
int left = 0;
int right = height.size() - 1;
while (left < right) {
int h = min(height[left], height[right]);
maxArea = max(maxArea, h * (right - left));
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
};
© 2015 - 2025 Weber Informatics LLC | Privacy Policy