Skip to content

Commit aee44b1

Browse files
authored
feat: add 14 more leetcode problems (#58)
1 parent 53a6695 commit aee44b1

File tree

104 files changed

+3242
-109
lines changed

Some content is hidden

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

104 files changed

+3242
-109
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= valid_sudoku
2+
PROBLEM ?= find_k_closest_elements
33
FORCE ?= 0
44
COMMA := ,
55

docs/llm-assisted-problem-creation.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ make p-test PROBLEM={problem_name}
121121

122122
- Assistant will automatically fix JSON template issues
123123
- Re-runs generation until linting passes
124-
- If JSON template fails after many iterations, ask agent to review the example template carefully as mentioned in the rules
125124

126125
**Test failures:**
127126

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Contiguous Array
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Hash Table, Prefix Sum
5+
**Tags:** grind
6+
7+
**LeetCode:** [Problem 525](https://leetcode.com/problems/contiguous-array/description/)
8+
9+
## Problem Description
10+
11+
Given a binary array `nums`, return _the maximum length of a contiguous subarray with an equal number of_ `0` _and_ `1`.
12+
13+
## Examples
14+
15+
### Example 1:
16+
17+
```
18+
Input: nums = [0,1]
19+
Output: 2
20+
Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
21+
```
22+
23+
### Example 2:
24+
25+
```
26+
Input: nums = [0,1,0]
27+
Output: 2
28+
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
29+
```
30+
31+
### Example 3:
32+
33+
```
34+
Input: nums = [0,1,1,1,1,1,0,0,0]
35+
Output: 6
36+
Explanation: [1,1,1,0,0,0] is the longest contiguous subarray with equal number of 0 and 1.
37+
```
38+
39+
## Constraints
40+
41+
- `1 <= nums.length <= 10^5`
42+
- `nums[i]` is either `0` or `1`.

leetcode/contiguous_array/__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_find_max_length(solution_class: type, nums: list[int]):
2+
implementation = solution_class()
3+
return implementation.find_max_length(nums)
4+
5+
6+
def assert_find_max_length(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.17.3
9+
# kernelspec:
10+
# display_name: leetcode-py-py3.13
11+
# language: python
12+
# name: python3
13+
# ---
14+
15+
# %%
16+
from helpers import assert_find_max_length, run_find_max_length
17+
from solution import Solution
18+
19+
# %%
20+
# Example test case
21+
nums = [0, 1]
22+
expected = 2
23+
24+
# %%
25+
result = run_find_max_length(Solution, nums)
26+
result
27+
28+
# %%
29+
assert_find_max_length(result, expected)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
3+
# Time: O(n)
4+
# Space: O(n)
5+
def find_max_length(self, nums: list[int]) -> int:
6+
count_map = {0: -1}
7+
count = 0
8+
max_len = 0
9+
10+
for i, num in enumerate(nums):
11+
count += 1 if num == 1 else -1
12+
13+
if count in count_map:
14+
max_len = max(max_len, i - count_map[count])
15+
else:
16+
count_map[count] = i
17+
18+
return max_len
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
from leetcode_py import logged_test
4+
5+
from .helpers import assert_find_max_length, run_find_max_length
6+
from .solution import Solution
7+
8+
9+
class TestContiguousArray:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@logged_test
14+
@pytest.mark.parametrize(
15+
"nums, expected",
16+
[
17+
([0, 1], 2),
18+
([0, 1, 0], 2),
19+
([0, 1, 1, 1, 1, 1, 0, 0, 0], 6),
20+
([0], 0),
21+
([1], 0),
22+
([0, 0], 0),
23+
([1, 1], 0),
24+
([0, 0, 1, 0, 0, 1, 1, 0], 6),
25+
([1, 0, 1, 0, 1], 4),
26+
([0, 1, 1, 0, 1, 1, 1, 0], 4),
27+
([1, 1, 1, 1, 1, 1, 1, 1], 0),
28+
([0, 0, 0, 0, 0, 0, 0, 0], 0),
29+
([1, 0, 1, 1, 0, 0, 1, 0, 1], 8),
30+
([0, 1, 0, 1, 0, 1, 0, 1], 8),
31+
([1, 0, 0, 1, 1, 0, 1, 0, 0, 1], 10),
32+
([0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0], 10),
33+
([1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 8),
34+
],
35+
)
36+
def test_find_max_length(self, nums: list[int], expected: int):
37+
result = run_find_max_length(Solution, nums)
38+
assert_find_max_length(result, expected)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Course Schedule II
2+
3+
**Difficulty:** Medium
4+
**Topics:** Depth-First Search, Breadth-First Search, Graph, Topological Sort
5+
**Tags:** grind
6+
7+
**LeetCode:** [Problem 210](https://leetcode.com/problems/course-schedule-ii/description/)
8+
9+
## Problem Description
10+
11+
There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`.
12+
13+
- For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`.
14+
15+
Return the ordering of courses you should take to finish all courses. If there are many valid answers, return **any** of them. If it is impossible to finish all courses, return **an empty array**.
16+
17+
## Examples
18+
19+
### Example 1:
20+
21+
```
22+
Input: numCourses = 2, prerequisites = [[1,0]]
23+
Output: [0,1]
24+
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].
25+
```
26+
27+
### Example 2:
28+
29+
```
30+
Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
31+
Output: [0,2,1,3]
32+
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
33+
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].
34+
```
35+
36+
### Example 3:
37+
38+
```
39+
Input: numCourses = 1, prerequisites = []
40+
Output: [0]
41+
```
42+
43+
## Constraints
44+
45+
- `1 <= numCourses <= 2000`
46+
- `0 <= prerequisites.length <= numCourses * (numCourses - 1)`
47+
- `prerequisites[i].length == 2`
48+
- `0 <= ai, bi < numCourses`
49+
- `ai != bi`
50+
- All the pairs `[ai, bi]` are **distinct**.

leetcode/course_schedule_ii/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)