mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2026-01-25 07:34:05 +00:00
day 27: 226-invert-binary-tree, 11-container-with-most-water
This commit is contained in:
10
11-container-with-most-water/driver.cpp
Normal file
10
11-container-with-most-water/driver.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> heights = {1, 8, 6, 2, 5, 4, 8};
|
||||
int answer = 40;
|
||||
Solution soln;
|
||||
cout << "Found largest container size correctly? " << soln.test(heights, answer) << endl;
|
||||
return 0;
|
||||
}
|
||||
21
11-container-with-most-water/soln.cpp
Normal file
21
11-container-with-most-water/soln.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "soln.hpp"
|
||||
|
||||
int Solution::maxArea(vector<int> &height)
|
||||
{
|
||||
int left = 0, right = height.size() - 1;
|
||||
int max_water = 0;
|
||||
while (left != right)
|
||||
{
|
||||
max_water = max(max_water, min(height[left], height[right]) * (right - left));
|
||||
if (height[left] > height[right])
|
||||
right--;
|
||||
else
|
||||
left++;
|
||||
}
|
||||
return max_water;
|
||||
}
|
||||
|
||||
bool Solution::test(vector<int> &height, int answer)
|
||||
{
|
||||
return maxArea(height) == answer;
|
||||
}
|
||||
8
11-container-with-most-water/soln.hpp
Normal file
8
11-container-with-most-water/soln.hpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
int maxArea(vector<int> &height);
|
||||
bool test(vector<int> &height, int answer);
|
||||
};
|
||||
Reference in New Issue
Block a user