Skip to content

Commit 6a6d97a

Browse files
committed
feat: add 5 more leetcode problems
1 parent 6c849c7 commit 6a6d97a

37 files changed

+1196
-3
lines changed

.cursor/commands/test-quality-assurance.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ make p-gen PROBLEM={problem_name} FORCE=1
2121
# Step 3: Restore original solution ONLY
2222
cp leetcode/{problem_name}_backup/solution.py leetcode/{problem_name}/solution.py
2323

24-
# Step 4: Verify mypy passes (CRITICAL for CI)
25-
poetry run mypy leetcode/{problem_name}/test_solution.py --explicit-package-bases --install-types --non-interactive --check-untyped-defs
24+
# Step 4: Verify linting pass (CRITICAL for CI)
25+
make p-lint PROBLEM={problem_name}
2626

2727
# Step 5: Verify tests pass (expected to fail if solution is incomplete)
2828
make p-test PROBLEM={problem_name}

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 ?= top_k_frequent_elements
2+
PROBLEM ?= palindromic_substrings
33
FORCE ?= 0
44
COMMA := ,
55

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Longest Repeating Character Replacement
2+
3+
**Difficulty:** Medium
4+
**Topics:** Hash Table, String, Sliding Window
5+
**Tags:** blind-75
6+
7+
**LeetCode:** [Problem 424](https://leetcode.com/problems/longest-repeating-character-replacement/description/)
8+
9+
## Problem Description
10+
11+
You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times.
12+
13+
Return the length of the longest substring containing the same letter you can get after performing the above operations.
14+
15+
## Examples
16+
17+
### Example 1:
18+
19+
```
20+
Input: s = "ABAB", k = 2
21+
Output: 4
22+
Explanation: Replace the two 'A's with two 'B's or vice versa.
23+
```
24+
25+
### Example 2:
26+
27+
```
28+
Input: s = "AABABBA", k = 1
29+
Output: 4
30+
Explanation: Replace the one 'A' in the middle with 'B' and form "AABBBBA".
31+
The substring "BBBB" has the longest repeating letters, which is 4.
32+
There may exists other ways to achieve this answer too.
33+
```
34+
35+
## Constraints
36+
37+
1 <= s.length <= 10^5
38+
s consists of only uppercase English letters.
39+
0 <= k <= s.length

leetcode/longest_repeating_character_replacement/__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_character_replacement(solution_class: type, s: str, k: int):
2+
implementation = solution_class()
3+
return implementation.characterReplacement(s, k)
4+
5+
6+
def assert_character_replacement(result: int, expected: int) -> bool:
7+
assert result == expected
8+
return True
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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_character_replacement, run_character_replacement
17+
from solution import Solution
18+
19+
# %%
20+
# Example test case
21+
s = "ABAB"
22+
k = 2
23+
expected = 4
24+
25+
# %%
26+
result = run_character_replacement(Solution, s, k)
27+
result
28+
29+
# %%
30+
assert_character_replacement(result, expected)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
3+
# Time: O(n) - single pass through string
4+
# Space: O(1) - at most 26 characters in count dict
5+
def characterReplacement(self, s: str, k: int) -> int:
6+
"""
7+
Find the length of the longest substring with same character
8+
after at most k replacements using sliding window approach.
9+
"""
10+
if not s:
11+
return 0
12+
13+
count: dict[str, int] = {}
14+
left = 0
15+
max_freq = 0
16+
max_length = 0
17+
18+
for right in range(len(s)):
19+
# Expand window: add character at right pointer
20+
count[s[right]] = count.get(s[right], 0) + 1
21+
max_freq = max(max_freq, count[s[right]])
22+
23+
# Shrink window if needed: if we need more than k replacements
24+
# Current window size = right - left + 1
25+
# Characters to replace = window_size - max_freq
26+
# If characters_to_replace > k, we need to shrink
27+
if (right - left + 1) - max_freq > k:
28+
count[s[left]] -= 1
29+
left += 1
30+
31+
# Update max length
32+
max_length = max(max_length, right - left + 1)
33+
34+
return max_length
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import pytest
2+
3+
from leetcode_py import logged_test
4+
5+
from .helpers import assert_character_replacement, run_character_replacement
6+
from .solution import Solution
7+
8+
9+
class TestLongestRepeatingCharacterReplacement:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@logged_test
14+
@pytest.mark.parametrize(
15+
"s, k, expected",
16+
[
17+
("ABAB", 2, 4),
18+
("AABABBA", 1, 4),
19+
("AAAA", 0, 4),
20+
("ABCDE", 0, 1),
21+
("ABCDE", 4, 5),
22+
("A", 0, 1),
23+
("A", 1, 1),
24+
("AAAB", 0, 3),
25+
("AAAB", 1, 4),
26+
("ABABAB", 2, 5),
27+
("ABABAB", 3, 6),
28+
("ABCDEF", 0, 1),
29+
("ABCDEF", 1, 2),
30+
("ABCDEF", 5, 6),
31+
("AABABBA", 2, 5),
32+
],
33+
)
34+
def test_character_replacement(self, s: str, k: int, expected: int):
35+
result = run_character_replacement(Solution, s, k)
36+
assert_character_replacement(result, expected)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Non-overlapping Intervals
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Dynamic Programming, Greedy, Sorting
5+
**Tags:** blind-75
6+
7+
**LeetCode:** [Problem 435](https://leetcode.com/problems/non-overlapping-intervals/description/)
8+
9+
## Problem Description
10+
11+
Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
12+
13+
Note that intervals which only touch at a point are non-overlapping. For example, [1, 2] and [2, 3] are non-overlapping.
14+
15+
## Examples
16+
17+
### Example 1:
18+
19+
```
20+
Input: intervals = [[1,2],[2,3],[3,4],[1,3]]
21+
Output: 1
22+
Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping.
23+
```
24+
25+
### Example 2:
26+
27+
```
28+
Input: intervals = [[1,2],[1,2],[1,2]]
29+
Output: 2
30+
Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping.
31+
```
32+
33+
### Example 3:
34+
35+
```
36+
Input: intervals = [[1,2],[2,3]]
37+
Output: 0
38+
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
39+
```
40+
41+
## Constraints
42+
43+
1 <= intervals.length <= 10^5
44+
intervals[i].length == 2
45+
-5 _ 10^4 <= starti < endi <= 5 _ 10^4

leetcode/non_overlapping_intervals/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)