day 27: 226-invert-binary-tree, 11-container-with-most-water

This commit is contained in:
2023-10-27 19:56:12 -07:00
parent 06ceedd3ca
commit d9298b9600
6 changed files with 115 additions and 0 deletions

View 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;
}

View 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;
}

View 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);
};