Skip to content

Commit 82f04fd

Browse files
anushkaGurjar99anushkagurj
andauthoredOct 16, 2020
11.Container with most water.cpp Leetcode solution file added (#114)
* 11.Container with most water.cpp solution file added * _322.cpp added (Coin change DP solution) Co-authored-by: anushkaGurjar1999 <[email protected]>
1 parent 85e2e08 commit 82f04fd

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed
 

‎cpp/_11.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// container-with-most-water
2+
// Problem Statement: https://leetcode.com/problems/container-with-most-water
3+
4+
#include<bits/stdc++.h>
5+
using namespace std;
6+
7+
class Solution {
8+
public:
9+
int maxArea(vector<int>& height) {
10+
if(height.size() < 1)
11+
return 0;
12+
13+
int left = 0;
14+
int right = height.size() - 1;
15+
int result = 0;
16+
17+
while(left < right){
18+
int area = (height[left] < height[right]) ? (height[left] * (right - left)) : (height[right] * (right -left));
19+
result = (area > result) ? area : result;
20+
(height[left] < height[right]) ? left++ : right--;
21+
}
22+
23+
return result;
24+
}
25+
};
26+

‎cpp/_322.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// coin-change
2+
// Problem Statement: https://leetcode.com/problems/coin-change/
3+
4+
class Solution{
5+
public:
6+
int coinChange(vector<int>& coins, int amount){
7+
8+
int MAX = amount + 1;
9+
vector<int> cache(amount + 1, MAX);
10+
11+
cache[0] = 0;
12+
for(auto coin : coins){
13+
for(int i = coin; i <= amount; i++)
14+
cache[i] = std::min(cache[i], cache[i - coin] + 1);
15+
}
16+
17+
return cache[amount] == MAX ? -1 : cache[amount];
18+
}
19+
};
20+

0 commit comments

Comments
 (0)
Please sign in to comment.