forked from AraragiSenpai7/Important-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerWithMostWater.cpp.txt
More file actions
29 lines (23 loc) · 931 Bytes
/
ContainerWithMostWater.cpp.txt
File metadata and controls
29 lines (23 loc) · 931 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Problem: Search in Rotated Sorted Array
// Solution by: Shaswat Shrivas
// Date: 01-10-2024
class Solution {
public:
int maxArea(vector<int>& height) {
int maxWater = 0, lp = 0, rp = height.size() - 1; // Initializing two pointers, one at the beginning (left pointer) and one at the end (right pointer) of the vector
// Loop until the two pointers meet
while (lp < rp) {
// Calculate the height of the container, which is the minimum height between the two pointers
int w = rp - lp;
int ht = min(height[lp], height[rp]);
// Update the maximum amount of water if the current amount is greater
maxWater = max(maxWater, w * ht);
if (height[lp] < height[rp]) {
lp++;
} else {
rp--;
}
}
return maxWater;
}
};