Skip to content

Commit 59a9d84

Browse files
committed
feat: add 10 more leetcode problems
1 parent 23deabe commit 59a9d84

73 files changed

Lines changed: 2600 additions & 143 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.claude/.dev/problem_lists/unscrapable.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
(253, "meeting-rooms-ii"),
99
(261, "graph-valid-tree"),
1010
(271, "encode-and-decode-strings"),
11+
(286, "walls-and-gates"),
1112
(323, "number-of-connected-components-in-an-undirected-graph"),
1213
(437, "path-sum-iii"), # Causing issues with next_problem.py - prefer manual creation
1314
# Add more unscrapable problems as discovered
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Best Time to Buy and Sell Stock with Cooldown
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Dynamic Programming
5+
**Tags:** neetcode-150
6+
7+
**LeetCode:** [Problem 309](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/)
8+
9+
## Problem Description
10+
11+
You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
12+
13+
Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the following restrictions:
14+
15+
- After you sell your stock, you cannot buy stock on the next day (i.e., cooldown one day).
16+
17+
**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
18+
19+
## Examples
20+
21+
### Example 1:
22+
23+
```
24+
Input: prices = [1,2,3,0,2]
25+
Output: 3
26+
```
27+
28+
**Explanation:** transactions = [buy, sell, cooldown, buy, sell]
29+
30+
### Example 2:
31+
32+
```
33+
Input: prices = [1]
34+
Output: 0
35+
```
36+
37+
## Constraints
38+
39+
- 1 <= prices.length <= 5000
40+
- 0 <= prices[i] <= 1000

leetcode/best_time_to_buy_and_sell_stock_with_cooldown/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def run_max_profit(solution_class: type, prices: list[int]):
2+
implementation = solution_class()
3+
return implementation.max_profit(prices)
4+
5+
6+
def assert_max_profit(result: int, expected: int) -> bool:
7+
assert result == expected
8+
return True
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# ---
2+
# jupyter:
3+
# jupytext:
4+
# text_representation:
5+
# extension: .py
6+
# format_name: percent
7+
# format_version: '1.3'
8+
# jupytext_version: 1.19.3
9+
# kernelspec:
10+
# display_name: leetcode-py-py3.13
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %%
16+
from helpers import assert_max_profit, run_max_profit
17+
from solution import Solution
18+
19+
# %%
20+
# Example test case
21+
prices = [1, 2, 3, 0, 2]
22+
expected = 3
23+
24+
# %%
25+
result = run_max_profit(Solution, prices)
26+
result
27+
28+
# %%
29+
assert_max_profit(result, expected)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(1)
4+
def max_profit(self, prices: list[int]) -> int:
5+
# State machine: held (own stock), sold (just sold -> cooldown), reset (no stock)
6+
held = float("-inf")
7+
sold = float("-inf")
8+
reset = 0
9+
for price in prices:
10+
prev_held, prev_sold, prev_reset = held, sold, reset
11+
held = max(prev_held, prev_reset - price)
12+
reset = max(prev_reset, prev_sold)
13+
sold = prev_held + price
14+
return int(max(sold, reset))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import pytest
2+
3+
from leetcode_py import logged_test
4+
5+
from .helpers import assert_max_profit, run_max_profit
6+
from .solution import Solution
7+
8+
9+
class TestBestTimeToBuyAndSellStockWithCooldown:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@logged_test
14+
@pytest.mark.parametrize(
15+
"prices, expected",
16+
[
17+
([1, 2, 3, 0, 2], 3),
18+
([1], 0),
19+
([1, 2], 1),
20+
([3, 2, 1], 0),
21+
([1, 2, 3], 2),
22+
([1, 2, 4], 3),
23+
([2, 4, 1], 2),
24+
([1, 4, 2], 3),
25+
([1, 2, 3, 0, 2, 5], 6),
26+
([6, 1, 6, 4, 2, 0, 1], 6),
27+
([5], 0),
28+
([1, 2, 3, 4, 5], 4),
29+
([1, 3, 1, 4], 3),
30+
([2, 1], 0),
31+
],
32+
)
33+
def test_max_profit(self, prices: list[int], expected: int):
34+
result = run_max_profit(Solution, prices)
35+
assert_max_profit(result, expected)

leetcode/burst_balloons/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Burst Balloons
2+
3+
**Difficulty:** Hard
4+
**Topics:** Array, Dynamic Programming
5+
**Tags:** neetcode-150
6+
7+
**LeetCode:** [Problem 312](https://leetcode.com/problems/burst-balloons/description/)
8+
9+
## Problem Description
10+
11+
You are given `n` balloons, indexed from `0` to `n - 1`. Each balloon is painted with a number on it represented by an array `nums`. You are asked to burst all the balloons.
12+
13+
If you burst the `ith` balloon, you will get `nums[i - 1] * nums[i] * nums[i + 1]` coins. If `i - 1` or `i + 1` goes out of bounds of the array, then treat it as if there is a balloon with a `1` painted on it.
14+
15+
Return _the maximum coins you can collect by bursting the balloons wisely_.
16+
17+
## Examples
18+
19+
### Example 1:
20+
21+
```
22+
Input: nums = [3,1,5,8]
23+
Output: 167
24+
```
25+
26+
**Explanation:**
27+
nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
28+
coins = 3*1*5 + 3*5*8 + 1*3*8 + 1*8*1 = 167
29+
30+
### Example 2:
31+
32+
```
33+
Input: nums = [1,5]
34+
Output: 10
35+
```
36+
37+
## Constraints
38+
39+
- n == nums.length
40+
- 1 <= n <= 300
41+
- 0 <= nums[i] <= 100

leetcode/burst_balloons/__init__.py

Whitespace-only changes.

leetcode/burst_balloons/helpers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def run_max_coins(solution_class: type, nums: list[int]):
2+
implementation = solution_class()
3+
return implementation.max_coins(nums)
4+
5+
6+
def assert_max_coins(result: int, expected: int) -> bool:
7+
assert result == expected
8+
return True

0 commit comments

Comments
 (0)