-
Notifications
You must be signed in to change notification settings - Fork 0
/
most_water_container.cpp
71 lines (64 loc) · 1.95 KB
/
most_water_container.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
static const int __ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
class Solution {
public:
// int maxArea(vector<int>& height) {
// int i,j;
// int n = height.size();
// vector<int> arr;
// //arr = height;
// int area;
// //sort(arr.end(), arr.begin());
// int start=0;
// int end= n-1;
// //for(i=0; i<n; i++){
// start = i;
// end = n-1;
// area = (end - start) * (min(height[start],height[end]));
// area = abs(area);
// arr.push_back(area);
// // for(j=i+1; j<n; j++){
// // if(i == j){
// // continue;
// // }
// // else{
// // area = (i - j) * (min(height[i],height[j]));
// // area = abs(area);
// // arr.push_back(area);
// // }
// // }
// //}
// sort(arr.begin(), arr.end());
// n = arr.size();
// return arr[n-1];
// }
int maxArea(vector<int> &height) {
int left = 0; // Left pointer starting from the leftmost edge
int right =
height.size() - 1; // Right pointer starting from the rightmost edge
int maxWater = 0; // Initialize the maximum water capacity
while (left < right) {
// Calculate the width of the container
int width = right - left;
// Calculate the height of the container (the minimum height between the
// two lines)
int h = min(height[left], height[right]);
// Calculate the water capacity of the current container
int water = width * h;
// Update the maximum water capacity if the current container holds more
// water
maxWater = max(maxWater, water);
// Move the pointers towards each other
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxWater;
}
};