-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11.cpp
More file actions
26 lines (26 loc) · 683 Bytes
/
11.cpp
File metadata and controls
26 lines (26 loc) · 683 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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (Leetcode) 11
// Title: Container With Most Water
// Link: https://leetcode.com/problems/container-with-most-water
// Idea: See LeetCode's explanations.
// Difficulty: medium
// Tags: arrays
class Solution {
public:
int maxArea(vector<int>& height) {
// Move the two sides closer and closer together
int max_area = 0;
int left = 0;
int right = height.size() - 1;
while (left != right) {
max_area =
max(max_area, (right - left) * min(height[left], height[right]));
if (height[left] < height[right]) {
++left;
} else {
--right;
}
}
return max_area;
}
};