Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .templates/leetcode/json/container_with_most_water.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"problem_name": "container_with_most_water",
"class_name": "ContainerWithMostWater",
"method_name": "max_area",
"problem_number": "11",
"problem_title": "Container With Most Water",
"difficulty": "Medium",
"topics": "Array, Two Pointers, Greedy",
"tags": ["grind-75"],
"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.",
"examples": [
{
"input": "height = [1,8,6,2,5,4,8,3,7]",
"output": "49",
"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."
},
{ "input": "height = [1,1]", "output": "1" }
],
"constraints": "- n == height.length\n- 2 <= n <= 10^5\n- 0 <= height[i] <= 10^4",
"parameters": "height: list[int]",
"return_type": "int",
"dummy_return": "0",
"imports": "",
"test_cases": [
{ "args": [[1, 8, 6, 2, 5, 4, 8, 3, 7]], "expected": 49 },
{ "args": [[1, 1]], "expected": 1 },
{ "args": [[1, 2, 1]], "expected": 2 },
{ "args": [[2, 3, 4, 5, 18, 17, 6]], "expected": 17 },
{ "args": [[1, 2, 4, 3]], "expected": 4 }
],
"param_names": "height, expected",
"param_names_with_types": "height: list[int], expected: int",
"input_description": "height={height}",
"input_params": "height",
"expected_param": "expected",
"method_args": "height",
"test_setup": "",
"test_logging": "",
"assertion_code": "assert result == expected",
"test_input_setup": "# Example test case\nheight = [1, 8, 6, 2, 5, 4, 8, 3, 7]",
"expected_output_setup": "expected = 49"
}
66 changes: 66 additions & 0 deletions .templates/leetcode/json/spiral_matrix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"problem_name": "spiral_matrix",
"class_name": "SpiralMatrix",
"method_name": "spiral_order",
"problem_number": "54",
"problem_title": "Spiral Matrix",
"difficulty": "Medium",
"topics": "Array, Matrix, Simulation",
"tags": ["grind-75"],
"problem_description": "Given an m x n matrix, return all elements of the matrix in spiral order.",
"examples": [
{ "input": "matrix = [[1,2,3],[4,5,6],[7,8,9]]", "output": "[1,2,3,6,9,8,7,4,5]" },
{
"input": "matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]",
"output": "[1,2,3,4,8,12,11,10,9,5,6,7]"
}
],
"constraints": "- m == matrix.length\n- n == matrix[i].length\n- 1 <= m, n <= 10\n- -100 <= matrix[i][j] <= 100",
"parameters": "matrix: list[list[int]]",
"return_type": "list[int]",
"dummy_return": "[]",
"imports": "",
"test_cases": [
{
"args": [
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
],
"expected": [1, 2, 3, 6, 9, 8, 7, 4, 5]
},
{
"args": [
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]
]
],
"expected": [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
},
{ "args": [[[1]]], "expected": [1] },
{
"args": [
[
[1, 2],
[3, 4]
]
],
"expected": [1, 2, 4, 3]
}
],
"param_names": "matrix, expected",
"param_names_with_types": "matrix: list[list[int]], expected: list[int]",
"input_description": "matrix={matrix}",
"input_params": "matrix",
"expected_param": "expected",
"method_args": "matrix",
"test_setup": "",
"test_logging": "",
"assertion_code": "assert result == expected",
"test_input_setup": "# Example test case\nmatrix = [[1,2,3],[4,5,6],[7,8,9]]",
"expected_output_setup": "expected = [1,2,3,6,9,8,7,4,5]"
}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PYTHON_VERSION = 3.13
PROBLEM ?= insert_interval
PROBLEM ?= spiral_matrix
FORCE ?= 0

sync_submodules:
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ Premium LeetCode practice repository with Python solutions, algorithm templates,

## 📋 Prerequisites

- Python 3.9+
- make
- git
- Optional: Graphviz for tree visualizations
- Python 3.13+
- make, git, Graphviz, poetry

## 🛠️ Installation

Expand Down
40 changes: 40 additions & 0 deletions leetcode/container_with_most_water/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 11. Container With Most Water

**Difficulty:** Medium
**Topics:** Array, Two Pointers, Greedy
**Tags:** grind-75
**LeetCode:** [Problem 11](https://leetcode.com/problems/container-with-most-water/description/)

## 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]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Notice that you may not slant the container.

## Examples

### Example 1:

![Example1](example1.png)

```
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
```

### Example 2:

```
Input: height = [1,1]
Output: 1
```

## Constraints

- n == height.length
- 2 <= n <= 10^5
- 0 <= height[i] <= 10^4
Empty file.
Binary file added leetcode/container_with_most_water/example1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions leetcode/container_with_most_water/playground.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "imports",
"metadata": {},
"outputs": [],
"source": [
"from solution import Solution"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "setup",
"metadata": {},
"outputs": [],
"source": [
"# Example test case\n",
"height = [1, 8, 6, 2, 5, 4, 8, 3, 7]\n",
"expected = 49"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "execute",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"49"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result = Solution().max_area(height)\n",
"result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "test",
"metadata": {},
"outputs": [],
"source": [
"assert result == expected"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "leetcode-py-py3.13",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
17 changes: 17 additions & 0 deletions leetcode/container_with_most_water/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
# Time: O(n)
# Space: O(1)
def max_area(self, height: list[int]) -> int:
left = 0
right = len(height) - 1
max_area_so_far = 0

while left < right:
area = min(height[left], height[right]) * (right - left)
max_area_so_far = max(area, max_area_so_far)
if height[right] > height[left]:
left += 1
else:
right -= 1

return max_area_so_far
28 changes: 28 additions & 0 deletions leetcode/container_with_most_water/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
from loguru import logger

from leetcode_py.test_utils import logged_test

from .solution import Solution


class TestContainerWithMostWater:
def setup_method(self):
self.solution = Solution()

@pytest.mark.parametrize(
"height, expected",
[
([1, 8, 6, 2, 5, 4, 8, 3, 7], 49),
([1, 1], 1),
([1, 2, 1], 2),
([2, 3, 4, 5, 18, 17, 6], 17),
([1, 2, 4, 3], 4),
],
)
@logged_test
def test_max_area(self, height: list[int], expected: int):
logger.info(f"Testing with height={height}")
result = self.solution.max_area(height)
logger.success(f"Got result: {result}")
assert result == expected
Loading
Loading