Skip to content

Commit 4a6e050

Browse files
authored
Create container-with-most-water.cpp
1 parent 2909b53 commit 4a6e050

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// https://leetcode.com/problems/container-with-most-water/
2+
#include<vector>
3+
4+
int maxArea(const std::vector<int>& height) {
5+
size_t startIndex = 0;
6+
size_t endIndex = height.size() - 1;
7+
8+
int result = INT_MIN;
9+
10+
while (startIndex < endIndex) {
11+
int currentHeigth = std::min(height[startIndex], height[endIndex]) * (endIndex - startIndex);
12+
13+
result = std::max(result, currentHeigth);
14+
15+
if (height[startIndex] > height[endIndex]) {
16+
--endIndex;
17+
}
18+
else {
19+
++startIndex;
20+
}
21+
}
22+
23+
return result;
24+
}

0 commit comments

Comments
 (0)