Skip to content

Commit 68db0be

Browse files
authored
feat: add 5 more leetcodes (#73)
1 parent 57a266b commit 68db0be

38 files changed

+1263
-1
lines changed

.cursor/.dev/update_tags.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"neetcode-150": [
3+
"binary_tree_maximum_path_sum",
4+
"find_minimum_in_rotated_sorted_array",
5+
"number_of_1_bits",
6+
"reorder_list",
7+
"reverse_bits"
8+
],
9+
"algo-master-75": ["binary_tree_maximum_path_sum"],
10+
"blind-75": [
11+
"binary_tree_maximum_path_sum",
12+
"find_minimum_in_rotated_sorted_array",
13+
"number_of_1_bits",
14+
"reorder_list",
15+
"reverse_bits"
16+
]
17+
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Binary Tree Maximum Path Sum
2+
3+
**Difficulty:** Hard
4+
**Topics:** Dynamic Programming, Tree, Depth-First Search, Binary Tree
5+
**Tags:** blind-75
6+
7+
**LeetCode:** [Problem 124](https://leetcode.com/problems/binary-tree-maximum-path-sum/description/)
8+
9+
## Problem Description
10+
11+
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
12+
13+
The **path sum** of a path is the sum of the node's values in the path.
14+
15+
Given the `root` of a binary tree, return _the maximum **path sum** of any **non-empty** path_.
16+
17+
## Examples
18+
19+
### Example 1:
20+
21+
![Example 1](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)
22+
23+
```
24+
Input: root = [1,2,3]
25+
Output: 6
26+
Explanation: The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
27+
```
28+
29+
### Example 2:
30+
31+
![Example 2](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)
32+
33+
```
34+
Input: root = [-10,9,20,null,null,15,7]
35+
Output: 42
36+
Explanation: The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
37+
```
38+
39+
## Constraints
40+
41+
- The number of nodes in the tree is in the range [1, 3 * 10^4].
42+
- -1000 <= Node.val <= 1000

leetcode/binary_tree_maximum_path_sum/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from leetcode_py import TreeNode
2+
3+
4+
def run_max_path_sum(solution_class: type, root_list: list[int | None]):
5+
root = TreeNode[int].from_list(root_list)
6+
implementation = solution_class()
7+
return implementation.max_path_sum(root)
8+
9+
10+
def assert_max_path_sum(result: int, expected: int) -> bool:
11+
assert result == expected
12+
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_max_path_sum, run_max_path_sum
17+
from solution import Solution
18+
19+
# %%
20+
# Example test case
21+
root_list: list[int | None] = [1, 2, 3]
22+
expected: int = 6
23+
24+
# %%
25+
result = run_max_path_sum(Solution, root_list)
26+
result
27+
28+
# %%
29+
assert_max_path_sum(result, expected)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from leetcode_py import TreeNode
2+
3+
4+
class Solution:
5+
6+
# Time: O(n) where n is the number of nodes
7+
# Space: O(h) where h is the height of the tree (recursion stack)
8+
def max_path_sum(self, root: TreeNode[int] | None) -> int:
9+
"""
10+
Find the maximum path sum in a binary tree.
11+
12+
A path is a sequence of nodes where each pair of adjacent nodes
13+
has an edge connecting them. A node can only appear once in the path.
14+
The path doesn't need to pass through the root.
15+
16+
Uses DFS with post-order traversal to calculate:
17+
1. Maximum path sum that can be extended upward from current node
18+
2. Maximum path sum that includes current node as the highest point
19+
"""
20+
if not root:
21+
return 0
22+
23+
max_sum = float("-inf")
24+
25+
def dfs(node: TreeNode[int] | None) -> int:
26+
nonlocal max_sum
27+
28+
if not node:
29+
return 0
30+
31+
# Get maximum path sum from left and right subtrees
32+
# If negative, we don't include them (take 0 instead)
33+
left_max = max(0, dfs(node.left))
34+
right_max = max(0, dfs(node.right))
35+
36+
# Current path sum if this node is the highest point
37+
# (left path + current node + right path)
38+
current_path_sum = node.val + left_max + right_max
39+
40+
# Update global maximum
41+
max_sum = max(max_sum, current_path_sum)
42+
43+
# Return maximum path sum that can be extended upward
44+
# (either left or right path + current node)
45+
return node.val + max(left_max, right_max)
46+
47+
dfs(root)
48+
return int(max_sum)
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_max_path_sum, run_max_path_sum
6+
from .solution import Solution
7+
8+
9+
class TestBinaryTreeMaximumPathSum:
10+
def setup_method(self):
11+
self.solution = Solution()
12+
13+
@logged_test
14+
@pytest.mark.parametrize(
15+
"root_list, expected",
16+
[
17+
([1, 2, 3], 6),
18+
([-10, 9, 20, None, None, 15, 7], 42),
19+
([1], 1),
20+
([-3], -3),
21+
([1, -2, 3], 4),
22+
([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1], 48),
23+
([1, 2, 3, 4, 5], 11),
24+
([-1, -2, -3], -1),
25+
([1, -1, 2], 3),
26+
([1, 2, -3, 4, 5], 11),
27+
([1, 2, 3, None, None, 4, 5], 12),
28+
([-1, 2, 3], 4),
29+
([1, -2, -3, 1, 3, -2, None, -1], 3),
30+
([2, -1], 2),
31+
([1, 2], 3),
32+
],
33+
)
34+
def test_max_path_sum(self, root_list: list[int | None], expected: int):
35+
result = run_max_path_sum(Solution, root_list)
36+
assert_max_path_sum(result, expected)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Find Minimum in Rotated Sorted Array
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Binary Search
5+
**Tags:** blind-75
6+
7+
**LeetCode:** [Problem 153](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
8+
9+
## Problem Description
10+
11+
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:
12+
13+
- `[4,5,6,7,0,1,2]` if it was rotated `4` times.
14+
- `[0,1,2,4,5,6,7]` if it was rotated `7` times.
15+
16+
Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.
17+
18+
Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_.
19+
20+
You must write an algorithm that runs in O(log n) time.
21+
22+
## Examples
23+
24+
### Example 1:
25+
26+
```
27+
Input: nums = [3,4,5,1,2]
28+
Output: 1
29+
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
30+
```
31+
32+
### Example 2:
33+
34+
```
35+
Input: nums = [4,5,6,7,0,1,2]
36+
Output: 0
37+
Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
38+
```
39+
40+
### Example 3:
41+
42+
```
43+
Input: nums = [11,13,15,17]
44+
Output: 11
45+
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
46+
```
47+
48+
## Constraints
49+
50+
- n == nums.length
51+
- 1 <= n <= 5000
52+
- -5000 <= nums[i] <= 5000
53+
- All the integers of nums are **unique**.
54+
- nums is sorted and rotated between 1 and n times.

leetcode/find_minimum_in_rotated_sorted_array/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)