mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
Initial commit
This commit is contained in:
26
Medium/container-with-most-water/solution.cpp
Normal file
26
Medium/container-with-most-water/solution.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int maxArea(std::vector<int> &height) {
|
||||
int left = 0;
|
||||
int right = height.size() - 1;
|
||||
int maxArea = 0;
|
||||
int curArea = 0;
|
||||
while (left < right) {
|
||||
curArea =
|
||||
(right - left) *
|
||||
(height[left] < height[right] ? height[left] : height[right]);
|
||||
maxArea = maxArea > curArea ? maxArea : curArea;
|
||||
|
||||
if (height[left] > height[right]) {
|
||||
--right;
|
||||
} else {
|
||||
++left;
|
||||
}
|
||||
}
|
||||
|
||||
return maxArea;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user