Skip to content

Commit 489a971

Browse files
authored
feat: add more problems (#12)
1 parent c2ba186 commit 489a971

File tree

20 files changed

+702
-27
lines changed

20 files changed

+702
-27
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"problem_name": "container_with_most_water",
3+
"class_name": "ContainerWithMostWater",
4+
"method_name": "max_area",
5+
"problem_number": "11",
6+
"problem_title": "Container With Most Water",
7+
"difficulty": "Medium",
8+
"topics": "Array, Two Pointers, Greedy",
9+
"tags": ["grind-75"],
10+
"problem_description": "You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).\n\nFind two lines that together with the x-axis form a container, such that the container contains the most water.\n\nReturn the maximum amount of water a container can store.\n\nNotice that you may not slant the container.",
11+
"examples": [
12+
{
13+
"input": "height = [1,8,6,2,5,4,8,3,7]",
14+
"output": "49",
15+
"explanation": "The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49."
16+
},
17+
{ "input": "height = [1,1]", "output": "1" }
18+
],
19+
"constraints": "- n == height.length\n- 2 <= n <= 10^5\n- 0 <= height[i] <= 10^4",
20+
"parameters": "height: list[int]",
21+
"return_type": "int",
22+
"dummy_return": "0",
23+
"imports": "",
24+
"test_cases": [
25+
{ "args": [[1, 8, 6, 2, 5, 4, 8, 3, 7]], "expected": 49 },
26+
{ "args": [[1, 1]], "expected": 1 },
27+
{ "args": [[1, 2, 1]], "expected": 2 },
28+
{ "args": [[2, 3, 4, 5, 18, 17, 6]], "expected": 17 },
29+
{ "args": [[1, 2, 4, 3]], "expected": 4 }
30+
],
31+
"param_names": "height, expected",
32+
"param_names_with_types": "height: list[int], expected: int",
33+
"input_description": "height={height}",
34+
"input_params": "height",
35+
"expected_param": "expected",
36+
"method_args": "height",
37+
"test_setup": "",
38+
"test_logging": "",
39+
"assertion_code": "assert result == expected",
40+
"test_input_setup": "# Example test case\nheight = [1, 8, 6, 2, 5, 4, 8, 3, 7]",
41+
"expected_output_setup": "expected = 49"
42+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"problem_name": "spiral_matrix",
3+
"class_name": "SpiralMatrix",
4+
"method_name": "spiral_order",
5+
"problem_number": "54",
6+
"problem_title": "Spiral Matrix",
7+
"difficulty": "Medium",
8+
"topics": "Array, Matrix, Simulation",
9+
"tags": ["grind-75"],
10+
"problem_description": "Given an m x n matrix, return all elements of the matrix in spiral order.",
11+
"examples": [
12+
{ "input": "matrix = [[1,2,3],[4,5,6],[7,8,9]]", "output": "[1,2,3,6,9,8,7,4,5]" },
13+
{
14+
"input": "matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]",
15+
"output": "[1,2,3,4,8,12,11,10,9,5,6,7]"
16+
}
17+
],
18+
"constraints": "- m == matrix.length\n- n == matrix[i].length\n- 1 <= m, n <= 10\n- -100 <= matrix[i][j] <= 100",
19+
"parameters": "matrix: list[list[int]]",
20+
"return_type": "list[int]",
21+
"dummy_return": "[]",
22+
"imports": "",
23+
"test_cases": [
24+
{
25+
"args": [
26+
[
27+
[1, 2, 3],
28+
[4, 5, 6],
29+
[7, 8, 9]
30+
]
31+
],
32+
"expected": [1, 2, 3, 6, 9, 8, 7, 4, 5]
33+
},
34+
{
35+
"args": [
36+
[
37+
[1, 2, 3, 4],
38+
[5, 6, 7, 8],
39+
[9, 10, 11, 12]
40+
]
41+
],
42+
"expected": [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
43+
},
44+
{ "args": [[[1]]], "expected": [1] },
45+
{
46+
"args": [
47+
[
48+
[1, 2],
49+
[3, 4]
50+
]
51+
],
52+
"expected": [1, 2, 4, 3]
53+
}
54+
],
55+
"param_names": "matrix, expected",
56+
"param_names_with_types": "matrix: list[list[int]], expected: list[int]",
57+
"input_description": "matrix={matrix}",
58+
"input_params": "matrix",
59+
"expected_param": "expected",
60+
"method_args": "matrix",
61+
"test_setup": "",
62+
"test_logging": "",
63+
"assertion_code": "assert result == expected",
64+
"test_input_setup": "# Example test case\nmatrix = [[1,2,3],[4,5,6],[7,8,9]]",
65+
"expected_output_setup": "expected = [1,2,3,6,9,8,7,4,5]"
66+
}

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

55
sync_submodules:

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ Premium LeetCode practice repository with Python solutions, algorithm templates,
1111

1212
## 📋 Prerequisites
1313

14-
- Python 3.9+
15-
- make
16-
- git
17-
- Optional: Graphviz for tree visualizations
14+
- Python 3.13+
15+
- make, git, Graphviz, poetry
1816

1917
## 🛠️ Installation
2018

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 11. Container With Most Water
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Two Pointers, Greedy
5+
**Tags:** grind-75
6+
**LeetCode:** [Problem 11](https://leetcode.com/problems/container-with-most-water/description/)
7+
8+
## Problem Description
9+
10+
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
11+
12+
Find two lines that together with the x-axis form a container, such that the container contains the most water.
13+
14+
Return the maximum amount of water a container can store.
15+
16+
Notice that you may not slant the container.
17+
18+
## Examples
19+
20+
### Example 1:
21+
22+
![Example1](example1.png)
23+
24+
```
25+
Input: height = [1,8,6,2,5,4,8,3,7]
26+
Output: 49
27+
```
28+
29+
### Example 2:
30+
31+
```
32+
Input: height = [1,1]
33+
Output: 1
34+
```
35+
36+
## Constraints
37+
38+
- n == height.length
39+
- 2 <= n <= 10^5
40+
- 0 <= height[i] <= 10^4

leetcode/container_with_most_water/__init__.py

Whitespace-only changes.
28.6 KB
Loading
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"height = [1, 8, 6, 2, 5, 4, 8, 3, 7]\n",
22+
"expected = 49"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 3,
28+
"id": "execute",
29+
"metadata": {},
30+
"outputs": [
31+
{
32+
"data": {
33+
"text/plain": [
34+
"49"
35+
]
36+
},
37+
"execution_count": 3,
38+
"metadata": {},
39+
"output_type": "execute_result"
40+
}
41+
],
42+
"source": [
43+
"result = Solution().max_area(height)\n",
44+
"result"
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": 4,
50+
"id": "test",
51+
"metadata": {},
52+
"outputs": [],
53+
"source": [
54+
"assert result == expected"
55+
]
56+
}
57+
],
58+
"metadata": {
59+
"kernelspec": {
60+
"display_name": "leetcode-py-py3.13",
61+
"language": "python",
62+
"name": "python3"
63+
},
64+
"language_info": {
65+
"codemirror_mode": {
66+
"name": "ipython",
67+
"version": 3
68+
},
69+
"file_extension": ".py",
70+
"mimetype": "text/x-python",
71+
"name": "python",
72+
"nbconvert_exporter": "python",
73+
"pygments_lexer": "ipython3",
74+
"version": "3.13.7"
75+
}
76+
},
77+
"nbformat": 4,
78+
"nbformat_minor": 5
79+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# Time: O(n)
3+
# Space: O(1)
4+
def max_area(self, height: list[int]) -> int:
5+
left = 0
6+
right = len(height) - 1
7+
max_area_so_far = 0
8+
9+
while left < right:
10+
area = min(height[left], height[right]) * (right - left)
11+
max_area_so_far = max(area, max_area_so_far)
12+
if height[right] > height[left]:
13+
left += 1
14+
else:
15+
right -= 1
16+
17+
return max_area_so_far
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 TestContainerWithMostWater:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@pytest.mark.parametrize(
14+
"height, expected",
15+
[
16+
([1, 8, 6, 2, 5, 4, 8, 3, 7], 49),
17+
([1, 1], 1),
18+
([1, 2, 1], 2),
19+
([2, 3, 4, 5, 18, 17, 6], 17),
20+
([1, 2, 4, 3], 4),
21+
],
22+
)
23+
@logged_test
24+
def test_max_area(self, height: list[int], expected: int):
25+
logger.info(f"Testing with height={height}")
26+
result = self.solution.max_area(height)
27+
logger.success(f"Got result: {result}")
28+
assert result == expected

0 commit comments

Comments
 (0)