Skip to content

Commit

Permalink
Implement Q43, 322
Browse files Browse the repository at this point in the history
  • Loading branch information
wshs0713 committed Feb 18, 2022
1 parent bf3f547 commit 4742520
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
19 changes: 18 additions & 1 deletion dynamic_programming/322_coin_change.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@

## Question

[\[Medium\] 322. Coin Change](https://leetcode.com/problems/coin-change/)
[[Medium] 322. Coin Change](https://leetcode.com/problems/coin-change/)

## Thought

## Code

```python
class Solution:
def coinChange(self, coins: List[int], amount: int) -> int:
dp = [-1]*(amount+1)

dp[0] = 0
for num in range(1, amount+1):
min_val = 2**31-1

for coin in coins:
if num - coin >= 0 and dp[num-coin] != -1:
min_val = min(min_val, dp[num-coin]+1)

if min_val == 2**31-1:
min_val = -1
dp[num] = min_val

return dp[amount]
```
25 changes: 24 additions & 1 deletion dynamic_programming/42_trapping_rain_water.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@

## Question

[\[Hard\] 42. Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)
[[Hard] 42. Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/)

## Thought

## Code

```python
class Solution:
def trap(self, height: List[int]) -> int:
area = 0
left_max, right_max = 0, 0
left, right = 0, len(height)-1

while left < right:
if height[left] < height[right]:
if height[left] >= left_max:
left_max = height[left]
else:
area += left_max - height[left]

left += 1
else:
if height[right] >= right_max:
right_max = height[right]
else:
area += right_max - height[right]

right -= 1

return area
```

0 comments on commit 4742520

Please sign in to comment.