Skip to content

Commit b995398

Browse files
committed
feat: add more questions
1 parent 1ef67fa commit b995398

File tree

8 files changed

+245
-7
lines changed

8 files changed

+245
-7
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"question_name": "insert_interval",
3+
"class_name": "InsertInterval",
4+
"method_name": "insert",
5+
"problem_number": "57",
6+
"problem_title": "Insert Interval",
7+
"difficulty": "Medium",
8+
"topics": "Array",
9+
"tags": ["grind-75"],
10+
"problem_description": "You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.\n\nInsert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).\n\nReturn intervals after the insertion.",
11+
"examples": [
12+
{ "input": "intervals = [[1,3],[6,9]], newInterval = [2,5]", "output": "[[1,5],[6,9]]" },
13+
{
14+
"input": "intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]",
15+
"output": "[[1,2],[3,10],[12,16]]"
16+
}
17+
],
18+
"constraints": "- 0 <= intervals.length <= 10^4\n- intervals[i].length == 2\n- 0 <= starti <= endi <= 10^5\n- intervals is sorted by starti in ascending order.\n- newInterval.length == 2\n- 0 <= start <= end <= 10^5",
19+
"parameters": "intervals: list[list[int]], newInterval: list[int]",
20+
"return_type": "list[list[int]]",
21+
"dummy_return": "[]",
22+
"imports": "",
23+
"test_cases": [
24+
{
25+
"args": [
26+
[
27+
[1, 3],
28+
[6, 9]
29+
],
30+
[2, 5]
31+
],
32+
"expected": [
33+
[1, 5],
34+
[6, 9]
35+
]
36+
},
37+
{
38+
"args": [
39+
[
40+
[1, 2],
41+
[3, 5],
42+
[6, 7],
43+
[8, 10],
44+
[12, 16]
45+
],
46+
[4, 8]
47+
],
48+
"expected": [
49+
[1, 2],
50+
[3, 10],
51+
[12, 16]
52+
]
53+
},
54+
{ "args": [[], [5, 7]], "expected": [[5, 7]] },
55+
{ "args": [[[1, 5]], [2, 3]], "expected": [[1, 5]] }
56+
],
57+
"param_names": "intervals, newInterval, expected",
58+
"param_names_with_types": "intervals: list[list[int]], newInterval: list[int], expected: list[list[int]]",
59+
"input_description": "intervals={intervals}, newInterval={newInterval}",
60+
"input_params": "intervals, newInterval",
61+
"expected_param": "expected",
62+
"method_args": "intervals, newInterval",
63+
"test_setup": "",
64+
"test_logging": "",
65+
"assertion_code": "assert result == expected",
66+
"test_input_setup": "# Example test case\\nintervals = [[1,3],[6,9]]\\nnewInterval = [2,5]",
67+
"expected_output_setup": "expected = [[1,5],[6,9]]"
68+
}

.templates/leetcode/{{cookiecutter.question_name}}/playground.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"metadata": {},
88
"outputs": [],
99
"source": [
10-
"from solution import Solution\n",{%- if cookiecutter.imports %}
11-
"\n",
10+
"from solution import Solution"{%- if cookiecutter.imports %},
11+
"",
1212
"{{cookiecutter.imports}}"{%- endif %}
1313
]
1414
},
@@ -19,8 +19,8 @@
1919
"metadata": {},
2020
"outputs": [],
2121
"source": [
22-
"# Example test case\n",
23-
"{{cookiecutter.test_input_setup.replace('# Example test case\\n', '')}}"
22+
{% for line in cookiecutter.test_input_setup.split('\n') %}"{{line}}"{% if not loop.last %},
23+
{% endif %}{% endfor %}
2424
]
2525
},
2626
{
@@ -40,7 +40,7 @@
4040
"metadata": {},
4141
"outputs": [],
4242
"source": [
43-
"result = Solution().{{cookiecutter.method_name}}({{cookiecutter.method_args}})\n",
43+
"result = Solution().{{cookiecutter.method_name}}({{cookiecutter.method_args}})",
4444
"result"
4545
]
4646
},
@@ -69,7 +69,7 @@
6969
"file_extension": ".py",
7070
"mimetype": "text/x-python",
7171
"name": "python",
72-
"nbconvert_exporter": "python",
72+
"nbconvert_exporter": "python3",
7373
"pygments_lexer": "ipython3",
7474
"version": "3.13.7"
7575
}

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-
QUESTION ?= reverse_linked_list_ii
2+
QUESTION ?= insert_interval
33
FORCE ?= 0
44

55
sync_submodules:

leetcode/insert_interval/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 57. Insert Interval
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array
5+
**Tags:** grind-75
6+
**LeetCode:** [Problem 57](https://leetcode.com/problems/insert-interval/description/)
7+
8+
## Problem Description
9+
10+
You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.
11+
12+
Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).
13+
14+
Return intervals after the insertion.
15+
16+
## Examples
17+
18+
### Example 1:
19+
20+
```
21+
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
22+
Output: [[1,5],[6,9]]
23+
```
24+
25+
### Example 2:
26+
27+
```
28+
Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
29+
Output: [[1,2],[3,10],[12,16]]
30+
```
31+
32+
## Constraints
33+
34+
- 0 <= intervals.length <= 10^4
35+
- intervals[i].length == 2
36+
- 0 <= starti <= endi <= 10^5
37+
- intervals is sorted by starti in ascending order.
38+
- newInterval.length == 2
39+
- 0 <= start <= end <= 10^5

leetcode/insert_interval/__init__.py

Whitespace-only changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"id": "fc4d8c0c",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": null,
16+
"id": "ecb1908a",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"intervals = [[1, 3], [6, 9]]\n",
22+
"newInterval = [2, 5]"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": null,
28+
"id": "ccd8921e",
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"expected = [[1, 5], [6, 9]]"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"id": "29433b11",
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"result = Solution().insert(intervals, newInterval)\n",
43+
"result"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": null,
49+
"id": "3a4e7961",
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"assert result == expected"
54+
]
55+
}
56+
],
57+
"metadata": {
58+
"kernelspec": {
59+
"display_name": "leetcode-py-py3.13",
60+
"language": "python",
61+
"name": "python3"
62+
},
63+
"language_info": {
64+
"codemirror_mode": {
65+
"name": "ipython",
66+
"version": 3
67+
},
68+
"file_extension": ".py",
69+
"mimetype": "text/x-python",
70+
"name": "python",
71+
"nbconvert_exporter": "python",
72+
"pygments_lexer": "ipython3",
73+
"version": "3.13.7"
74+
}
75+
},
76+
"nbformat": 4,
77+
"nbformat_minor": 5
78+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(n)
4+
def insert(self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:
5+
result = []
6+
i = 0
7+
8+
# Add intervals before newInterval
9+
while i < len(intervals) and intervals[i][1] < newInterval[0]:
10+
result.append(intervals[i])
11+
i += 1
12+
13+
# Merge overlapping intervals
14+
while i < len(intervals) and intervals[i][0] <= newInterval[1]:
15+
newInterval[0] = min(newInterval[0], intervals[i][0])
16+
newInterval[1] = max(newInterval[1], intervals[i][1])
17+
i += 1
18+
19+
result.append(newInterval)
20+
21+
# Add remaining intervals
22+
while i < len(intervals):
23+
result.append(intervals[i])
24+
i += 1
25+
26+
return result

leetcode/insert_interval/tests.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import pytest
2+
from loguru import logger
3+
4+
from leetcode_py.test_utils import logged_test
5+
6+
from .solution import Solution
7+
8+
9+
class TestInsertInterval:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@pytest.mark.parametrize(
14+
"intervals, newInterval, expected",
15+
[
16+
([[1, 3], [6, 9]], [2, 5], [[1, 5], [6, 9]]),
17+
([[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], [4, 8], [[1, 2], [3, 10], [12, 16]]),
18+
([], [5, 7], [[5, 7]]),
19+
([[1, 5]], [2, 3], [[1, 5]]),
20+
],
21+
)
22+
@logged_test
23+
def test_insert(self, intervals: list[list[int]], newInterval: list[int], expected: list[list[int]]):
24+
logger.info(f"Testing with intervals={intervals}, newInterval={newInterval}")
25+
result = self.solution.insert(intervals, newInterval)
26+
logger.success(f"Got result: {result}")
27+
assert result == expected

0 commit comments

Comments
 (0)