-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_with_most_water.cpp
More file actions
75 lines (57 loc) · 2 KB
/
container_with_most_water.cpp
File metadata and controls
75 lines (57 loc) · 2 KB
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
72
73
74
75
/*
11. Container With Most Water
Time Complexity: O(n)
Space Complexity: O(1)
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
int maxArea(vector<int>& height) {
int left = 0;
int right = height.size() - 1;
int maxArea = 0;
while (left < right) {
int width = right - left;
int currentHeight = min(height[left], height[right]);
int currentArea = width * currentHeight;
maxArea = max(maxArea, currentArea);
if (height[left] < height[right]) {
left++;
} else {
right--;
}
}
return maxArea;
}
};
// Test cases
int main() {
Solution solution;
// Example 1
vector<int> height1 = {1, 8, 6, 2, 5, 4, 8, 3, 7};
cout << "Example 1: " << solution.maxArea(height1) << endl; // Expected: 49
// Max area between index 1 (height 8) and index 8 (height 7)
// Area = 7 * min(8, 7) = 7 * 7 = 49
// Example 2
vector<int> height2 = {1, 1};
cout << "Example 2: " << solution.maxArea(height2) << endl; // Expected: 1
// Edge case: two different heights
vector<int> height3 = {1, 2};
cout << "Example 3: " << solution.maxArea(height3) << endl; // Expected: 1
// Edge case: increasing heights
vector<int> height4 = {1, 2, 3, 4, 5};
cout << "Example 4: " << solution.maxArea(height4) << endl; // Expected: 6
// Edge case: decreasing heights
vector<int> height5 = {5, 4, 3, 2, 1};
cout << "Example 5: " << solution.maxArea(height5) << endl; // Expected: 6
// Edge case: all same height
vector<int> height6 = {3, 3, 3, 3};
cout << "Example 6: " << solution.maxArea(height6) << endl; // Expected: 9
// Edge case: tall on edges
vector<int> height7 = {9, 1, 1, 1, 9};
cout << "Example 7: " << solution.maxArea(height7) << endl; // Expected: 36
return 0;
}