Skip to content

Commit fe5c8a6

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 309, 337 and 714
1 parent cf47e7c commit fe5c8a6

7 files changed

+180
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
203203
- [290 Word Pattern](https://leetcode.com/problems/word-pattern/description/)
204204
- [295 Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/description/)
205205
- [300 Longest Increasing Subsequence](https://leetcode.com/problems/longest-increasing-subsequence/description/)
206+
- [309 Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
206207
- [322 Coin Change](https://leetcode.com/problems/coin-change/description/)
208+
- [337 House Robber III](https://leetcode.com/problems/house-robber-iii/description/)
207209
- [373 Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/description/)
208210
- [377 Combination Sum IV](https://leetcode.com/problems/combination-sum-iv/description/)
209211
- [380 Insert Delete GetRandom O(1)](https://leetcode.com/problems/insert-delete-getrandom-o1/description/)
@@ -222,6 +224,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
222224
- [637 Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/)
223225
- [673 Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/)
224226
- [712 Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/description/)
227+
- [714 Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/)
225228
- [740 Delete and Earn](https://leetcode.com/problems/delete-and-earn/description/)
226229
- [746 Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/description/)
227230
- [790 Domino and Tromino Tiling](https://leetcode.com/problems/domino-and-tromino-tiling/description/)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def maxProfit(self, prices: List[int]) -> int:
8+
"""
9+
You are given an array prices where prices[i] is the price of a given stock on
10+
the ith day.
11+
12+
Find the maximum profit you can achieve. You may complete as many transactions
13+
as you like (i.e., buy one and sell one share of the stock multiple times) with
14+
the following restrictions:
15+
- After you sell your stock, you cannot buy stock on the next day
16+
(i.e., cooldown one day).
17+
18+
Note: You may not engage in multiple transactions simultaneously (i.e., you
19+
must sell the stock before you buy again).
20+
"""
21+
dp = {}
22+
23+
def dfs(i: int, buy: bool):
24+
if i >= len(prices):
25+
return 0
26+
if (i, buy) in dp:
27+
return dp[(i, buy)]
28+
if buy:
29+
# Case: Either BUY or COOLDOWN
30+
dp[(i, buy)] = max(
31+
dfs(i + 1, not buy) - prices[i], # BUY
32+
dfs(i + 1, buy), # COOLDOWN
33+
)
34+
else:
35+
# Case: Either SELL + COOLDOWN or COOLDOWN
36+
dp[(i, buy)] = max(
37+
dfs(i + 2, not buy) + prices[i], # SELL + COOLDOWN
38+
dfs(i + 1, buy), # COOLDOWN
39+
)
40+
return dp[(i, buy)]
41+
42+
return dfs(0, True)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Optional, Tuple
2+
3+
from awesome_python_leetcode.tree import TreeNode
4+
5+
6+
class Solution:
7+
"""Base class for all LeetCode Problems."""
8+
9+
def rob(self, root: Optional[TreeNode]) -> int:
10+
"""
11+
The thief has found himself a new place for his thievery again. There is only
12+
one entrance to this area, called root.
13+
14+
Besides the root, each house has one and only one parent house. After a tour,
15+
the smart thief realized that all houses in this place form a binary tree. It
16+
will automatically contact the police if two directly-linked houses were broken
17+
into on the same night.
18+
19+
Given the root of the binary tree, return the maximum amount of money the thief
20+
can rob without alerting the police.
21+
"""
22+
23+
def dfs(root: Optional[TreeNode]) -> Tuple[int, int]:
24+
if root is None:
25+
return 0, 0
26+
left_first, left_second = dfs(root.left)
27+
right_first, right_second = dfs(root.right)
28+
tmp = max(root.val + left_second + right_second, left_first + right_first)
29+
second = left_first + right_first
30+
first = tmp
31+
return first, second
32+
33+
return dfs(root)[0]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def maxProfit(self, prices: List[int], fee: int) -> int:
8+
"""
9+
You are given an array prices where prices[i] is the price of a given stock on
10+
the ith day, and an integer fee representing a transaction fee.
11+
12+
Find the maximum profit you can achieve. You may complete as many transactions
13+
as you like, but you need to pay the transaction fee for each transaction.
14+
15+
Note:
16+
- You may not engage in multiple transactions simultaneously (i.e., you must
17+
sell the stock before you buy again).
18+
- The transaction fee is only charged once for each stock purchase and sale.
19+
"""
20+
dp = {}
21+
22+
def dfs(i: int, buy: bool):
23+
if i >= len(prices):
24+
return 0
25+
if (i, buy) in dp:
26+
return dp[(i, buy)]
27+
if buy:
28+
# Case: Either BUY or SKIP
29+
dp[(i, buy)] = max(
30+
dfs(i + 1, not buy) - prices[i], # BUY
31+
dfs(i + 1, buy), # SKIP
32+
)
33+
else:
34+
# Case: Either SELL or SKIP
35+
dp[(i, buy)] = max(
36+
dfs(i + 1, not buy) + prices[i] - fee, # SELL
37+
dfs(i + 1, buy), # SKIP
38+
)
39+
return dp[(i, buy)]
40+
41+
return dfs(0, True)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._309_best_time_to_buy_and_sell_stock_with_cooldown import (
6+
Solution,
7+
)
8+
9+
10+
@pytest.mark.parametrize(
11+
argnames=["prices", "expected"],
12+
argvalues=[
13+
([1, 2, 3, 0, 2], 3),
14+
([1], 0),
15+
],
16+
)
17+
def test_func(prices: List[int], expected: int):
18+
"""Tests the solution of a LeetCode problem."""
19+
max_profit = Solution().maxProfit(prices)
20+
assert max_profit == expected

tests/test_337_house_robber_III.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._337_house_robber_III import Solution
6+
from awesome_python_leetcode.tree import TreeNode
7+
8+
9+
@pytest.mark.parametrize(
10+
argnames=["root", "expected"],
11+
argvalues=[
12+
([3, 2, 3, None, 3, None, 1], 7),
13+
([3, 4, 5, 1, 3, None, 1], 9),
14+
],
15+
)
16+
def test_func(root: List[int], expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
root = TreeNode.build(root)
19+
max_amount = Solution().rob(root)
20+
assert max_amount == expected
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# flake8: noqa
2+
from typing import List
3+
4+
import pytest
5+
6+
from awesome_python_leetcode._714_best_time_to_buy_and_sell_stock_with_transaction_fee import (
7+
Solution,
8+
)
9+
10+
11+
@pytest.mark.parametrize(
12+
argnames=["prices", "fee", "expected"],
13+
argvalues=[
14+
([1, 3, 2, 8, 4, 9], 2, 8),
15+
([1, 3, 7, 5, 10, 3], 3, 6),
16+
],
17+
)
18+
def test_func(prices: List[int], fee: int, expected: int):
19+
"""Tests the solution of a LeetCode problem."""
20+
max_profit = Solution().maxProfit(prices, fee)
21+
assert max_profit == expected

0 commit comments

Comments
 (0)