Skip to content

Commit ea66472

Browse files
authored
Added tasks 117-125
1 parent c81ed06 commit ea66472

File tree

17 files changed

+456
-0
lines changed

17 files changed

+456
-0
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
244244
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
245245
|-|-|-|-|-|-
246246
| 0121 |[Best Time to Buy and Sell Stock](src/main/ts/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 96.44
247+
| 0122 |[Best Time to Buy and Sell Stock II](src/main/ts/g0101_0200/s0122_best_time_to_buy_and_sell_stock_ii/solution.ts)| Medium | Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 0 | 100.00
247248

248249
#### Day 8
249250

@@ -277,6 +278,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
277278

278279
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
279280
|-|-|-|-|-|-
281+
| 0120 |[Triangle](src/main/ts/g0101_0200/s0120_triangle/solution.ts)| Medium | Array, Dynamic_Programming | 0 | 100.00
280282

281283
#### Day 14
282284

@@ -884,6 +886,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
884886
| 0283 |[Move Zeroes](src/main/ts/g0201_0300/s0283_move_zeroes/solution.ts)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 1 | 82.86
885887
| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 1 | 89.70
886888
| 0058 |[Length of Last Word](src/main/ts/g0001_0100/s0058_length_of_last_word/solution.ts)| Easy | String | 0 | 100.00
889+
| 0122 |[Best Time to Buy and Sell Stock II](src/main/ts/g0101_0200/s0122_best_time_to_buy_and_sell_stock_ii/solution.ts)| Medium | Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 0 | 100.00
887890
| 0080 |[Remove Duplicates from Sorted Array II](src/main/ts/g0001_0100/s0080_remove_duplicates_from_sorted_array_ii/solution.ts)| Medium | Array, Two_Pointers | 40 | 99.63
888891
| 0189 |[Rotate Array](src/main/ts/g0101_0200/s0189_rotate_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 1 | 86.17
889892
| 0055 |[Jump Game](src/main/ts/g0001_0100/s0055_jump_game/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
@@ -897,6 +900,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
897900

898901
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
899902
|-|-|-|-|-|-
903+
| 0125 |[Valid Palindrome](src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers | 0 | 100.00
900904
| 0026 |[Remove Duplicates from Sorted Array](src/main/ts/g0001_0100/s0026_remove_duplicates_from_sorted_array/solution.ts)| Easy | Top_Interview_Questions, Array, Two_Pointers | 0 | 100.00
901905
| 0042 |[Trapping Rain Water](src/main/ts/g0001_0100/s0042_trapping_rain_water/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
902906
| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 30 | 91.56
@@ -971,6 +975,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
971975

972976
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
973977
|-|-|-|-|-|-
978+
| 0120 |[Triangle](src/main/ts/g0101_0200/s0120_triangle/solution.ts)| Medium | Array, Dynamic_Programming | 0 | 100.00
974979
| 0139 |[Word Break](src/main/ts/g0101_0200/s0139_word_break/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 4 | 76.41
975980
| 0152 |[Maximum Product Subarray](src/main/ts/g0101_0200/s0152_maximum_product_subarray/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
976981
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
@@ -1016,6 +1021,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10161021
| 0169 |[Majority Element](src/main/ts/g0101_0200/s0169_majority_element/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10171022
| 0189 |[Rotate Array](src/main/ts/g0101_0200/s0189_rotate_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 1 | 86.17
10181023
| 0121 |[Best Time to Buy and Sell Stock](src/main/ts/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 96.44
1024+
| 0122 |[Best Time to Buy and Sell Stock II](src/main/ts/g0101_0200/s0122_best_time_to_buy_and_sell_stock_ii/solution.ts)| Medium | Top_Interview_Questions, Array, Dynamic_Programming, Greedy | 0 | 100.00
10191025
| 0055 |[Jump Game](src/main/ts/g0001_0100/s0055_jump_game/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10201026
| 0045 |[Jump Game II](src/main/ts/g0001_0100/s0045_jump_game_ii/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 1 | 84.27
10211027
| 0238 |[Product of Array Except Self](src/main/ts/g0201_0300/s0238_product_of_array_except_self/solution.ts)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 3 | 92.81
@@ -1032,6 +1038,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10321038

10331039
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10341040
|-|-|-|-|-|-
1041+
| 0125 |[Valid Palindrome](src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers | 0 | 100.00
10351042
| 0011 |[Container With Most Water](src/main/ts/g0001_0100/s0011_container_with_most_water/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 2 | 80.13
10361043
| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 30 | 91.56
10371044

@@ -1100,6 +1107,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11001107
| 0101 |[Symmetric Tree](src/main/ts/g0101_0200/s0101_symmetric_tree/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(log(N)) | 0 | 100.00
11011108
| 0105 |[Construct Binary Tree from Preorder and Inorder Traversal](src/main/ts/g0101_0200/s0105_construct_binary_tree_from_preorder_and_inorder_traversal/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer, Big_O_Time_O(N)_Space_O(N) | 2 | 93.38
11021109
| 0106 |[Construct Binary Tree from Inorder and Postorder Traversal](src/main/ts/g0101_0200/s0106_construct_binary_tree_from_inorder_and_postorder_traversal/solution.ts)| Medium | Array, Hash_Table, Tree, Binary_Tree, Divide_and_Conquer | 1 | 98.78
1110+
| 0117 |[Populating Next Right Pointers in Each Node II](src/main/ts/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii/solution.ts)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List | 47 | 97.74
11031111
| 0114 |[Flatten Binary Tree to Linked List](src/main/ts/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
11041112
| 0112 |[Path Sum](src/main/ts/g0101_0200/s0112_path_sum/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
11051113
| 0124 |[Binary Tree Maximum Path Sum](src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 2 | 71.11
@@ -1211,11 +1219,13 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12111219

12121220
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12131221
|-|-|-|-|-|-
1222+
| 0120 |[Triangle](src/main/ts/g0101_0200/s0120_triangle/solution.ts)| Medium | Array, Dynamic_Programming | 0 | 100.00
12141223
| 0064 |[Minimum Path Sum](src/main/ts/g0001_0100/s0064_minimum_path_sum/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 4 | 70.73
12151224
| 0063 |[Unique Paths II](src/main/ts/g0001_0100/s0063_unique_paths_ii/solution.ts)| Medium | Array, Dynamic_Programming, Matrix | 0 | 100.00
12161225
| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 8 | 99.14
12171226
| 0097 |[Interleaving String](src/main/ts/g0001_0100/s0097_interleaving_string/solution.ts)| Medium | String, Dynamic_Programming | 43 | 97.65
12181227
| 0072 |[Edit Distance](src/main/ts/g0001_0100/s0072_edit_distance/solution.ts)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 6 | 93.83
1228+
| 0123 |[Best Time to Buy and Sell Stock III](src/main/ts/g0101_0200/s0123_best_time_to_buy_and_sell_stock_iii/solution.ts)| Hard | Array, Dynamic_Programming | 5 | 85.07
12191229
| 0221 |[Maximal Square](src/main/ts/g0201_0300/s0221_maximal_square/solution.ts)| Medium | Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 18 | 59.02
12201230

12211231
### Data Structure I
@@ -1512,6 +1522,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
15121522
|-|-|-|-|-|-
15131523
| 0070 |[Climbing Stairs](src/main/ts/g0001_0100/s0070_climbing_stairs/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
15141524
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1525+
| 0120 |[Triangle](src/main/ts/g0101_0200/s0120_triangle/solution.ts)| Medium | Array, Dynamic_Programming | 0 | 100.00
15151526

15161527
#### Day 13 Bit Manipulation
15171528

@@ -1569,6 +1580,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
15691580

15701581
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15711582
|-|-|-|-|-|-
1583+
| 0117 |[Populating Next Right Pointers in Each Node II](src/main/ts/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii/solution.ts)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List | 47 | 97.74
15721584

15731585
#### Day 8 Breadth First Search Depth First Search
15741586

@@ -1705,8 +1717,13 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
17051717
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Data_Structure_II_Day_1_Array, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Integers, Top_Interview_150_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 78.27
17061718
| 0131 |[Palindrome Partitioning](src/main/ts/g0101_0200/s0131_palindrome_partitioning/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(N\*2^N)_Space_O(2^N\*N) | 13 | 94.96
17071719
| 0128 |[Longest Consecutive Sequence](src/main/ts/g0101_0200/s0128_longest_consecutive_sequence/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Union_Find, Top_Interview_150_Hashmap, Big_O_Time_O(N_log_N)_Space_O(1) | 34 | 90.07
1720+
| 0125 |[Valid Palindrome](src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers, Udemy_Two_Pointers, Top_Interview_150_Two_Pointers | 0 | 100.00
17081721
| 0124 |[Binary Tree Maximum Path Sum](src/main/ts/g0101_0200/s0124_binary_tree_maximum_path_sum/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(N) | 2 | 71.11
1722+
| 0123 |[Best Time to Buy and Sell Stock III](src/main/ts/g0101_0200/s0123_best_time_to_buy_and_sell_stock_iii/solution.ts)| Hard | Array, Dynamic_Programming, Top_Interview_150_Multidimensional_DP | 5 | 85.07
1723+
| 0122 |[Best Time to Buy and Sell Stock II](src/main/ts/g0101_0200/s0122_best_time_to_buy_and_sell_stock_ii/solution.ts)| Medium | Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Dynamic_Programming_I_Day_7, Udemy_Arrays, Top_Interview_150_Array/String | 0 | 100.00
17091724
| 0121 |[Best Time to Buy and Sell Stock](src/main/ts/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Data_Structure_I_Day_3_Array, Dynamic_Programming_I_Day_7, Level_1_Day_5_Greedy, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(N)_Space_O(1) | 1 | 96.44
1725+
| 0120 |[Triangle](src/main/ts/g0101_0200/s0120_triangle/solution.ts)| Medium | Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_13, Udemy_Dynamic_Programming, Top_Interview_150_Multidimensional_DP | 0 | 100.00
1726+
| 0117 |[Populating Next Right Pointers in Each Node II](src/main/ts/g0101_0200/s0117_populating_next_right_pointers_in_each_node_ii/solution.ts)| Medium | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Linked_List, Algorithm_II_Day_7_Breadth_First_Search_Depth_First_Search, Top_Interview_150_Binary_Tree_General | 47 | 97.74
17101727
| 0114 |[Flatten Binary Tree to Linked List](src/main/ts/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Udemy_Linked_List, Top_Interview_150_Binary_Tree_General, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
17111728
| 0112 |[Path Sum](src/main/ts/g0101_0200/s0112_path_sum/solution.ts)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Data_Structure_I_Day_12_Tree, Top_Interview_150_Binary_Tree_General | 0 | 100.00
17121729
| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/ts/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/solution.ts)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer, Data_Structure_II_Day_15_Tree, Level_2_Day_9_Binary_Search_Tree, Udemy_Tree_Stack_Queue, Top_Interview_150_Divide_and_Conquer | 0 | 100.00
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class _Node {
2+
val: number
3+
left: _Node | null
4+
right: _Node | null
5+
next: _Node | null
6+
7+
constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) {
8+
this.val = val === undefined ? 0 : val
9+
this.left = left === undefined ? null : left
10+
this.right = right === undefined ? null : right
11+
this.next = next === undefined ? null : next
12+
}
13+
}
14+
15+
export { _Node }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
117\. Populating Next Right Pointers in Each Node II
2+
3+
Medium
4+
5+
Given a binary tree
6+
7+
struct Node { int val; Node \*left; Node \*right; Node \*next; }
8+
9+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.
10+
11+
Initially, all next pointers are set to `NULL`.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2019/02/15/117_sample.png)
16+
17+
**Input:** root = [1,2,3,4,5,null,7]
18+
19+
**Output:** [1,#,2,3,#,4,5,7,#]
20+
21+
**Explanation:** Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with '#' signifying the end of each level.
22+
23+
**Example 2:**
24+
25+
**Input:** root = []
26+
27+
**Output:** []
28+
29+
**Constraints:**
30+
31+
* The number of nodes in the tree is in the range `[0, 6000]`.
32+
* `-100 <= Node.val <= 100`
33+
34+
**Follow-up:**
35+
36+
* You may only use constant extra space.
37+
* The recursive approach is fine. You may assume implicit stack space does not count as extra space for this problem.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// #Medium #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree #Linked_List
2+
// #Algorithm_II_Day_7_Breadth_First_Search_Depth_First_Search
3+
// #Top_Interview_150_Binary_Tree_General #2025_04_07_Time_47_ms_(97.74%)_Space_61.19_MB_(53.38%)
4+
5+
import { _Node } from "../../com_github_leetcode/_node"
6+
7+
/**
8+
* Definition for _Node.
9+
* class _Node {
10+
* val: number
11+
* left: _Node | null
12+
* right: _Node | null
13+
* next: _Node | null
14+
*
15+
* constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) {
16+
* this.val = (val===undefined ? 0 : val)
17+
* this.left = (left===undefined ? null : left)
18+
* this.right = (right===undefined ? null : right)
19+
* this.next = (next===undefined ? null : next)
20+
* }
21+
* }
22+
*/
23+
function connect(root: _Node | null): _Node | null {
24+
if (!root) {
25+
return null
26+
}
27+
root.next = null
28+
let dummyHead = new _Node()
29+
let current: _Node | null = root
30+
while (current) {
31+
// reset dummyHead
32+
dummyHead.next = null
33+
let child = dummyHead
34+
// traverse the current level
35+
while (current) {
36+
if (current.left) {
37+
child.next = current.left
38+
child = child.next
39+
}
40+
if (current.right) {
41+
child.next = current.right
42+
child = child.next
43+
}
44+
current = current.next
45+
}
46+
// move on to the next level
47+
current = dummyHead.next
48+
}
49+
return root
50+
}
51+
52+
export { connect }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
120\. Triangle
2+
3+
Medium
4+
5+
Given a `triangle` array, return _the minimum path sum from top to bottom_.
6+
7+
For each step, you may move to an adjacent number of the row below. More formally, if you are on index `i` on the current row, you may move to either index `i` or index `i + 1` on the next row.
8+
9+
**Example 1:**
10+
11+
**Input:** triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
12+
13+
**Output:** 11
14+
15+
**Explanation:**
16+
17+
The triangle looks like:
18+
2
19+
3 4
20+
6 5 7
21+
4 1 8 3
22+
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
23+
24+
**Example 2:**
25+
26+
**Input:** triangle = [[-10]]
27+
28+
**Output:** -10
29+
30+
**Constraints:**
31+
32+
* `1 <= triangle.length <= 200`
33+
* `triangle[0].length == 1`
34+
* `triangle[i].length == triangle[i - 1].length + 1`
35+
* <code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code>
36+
37+
**Follow up:** Could you do this using only `O(n)` extra space, where `n` is the total number of rows in the triangle?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #Medium #Array #Dynamic_Programming #Algorithm_I_Day_12_Dynamic_Programming
2+
// #Dynamic_Programming_I_Day_13 #Udemy_Dynamic_Programming #Top_Interview_150_Multidimensional_DP
3+
// #2025_04_07_Time_0_ms_(100.00%)_Space_58.24_MB_(28.24%)
4+
5+
function minimumTotal(triangle: number[][]): number {
6+
if (!triangle || triangle.length === 0) {
7+
return 0
8+
}
9+
const rows = triangle.length
10+
const cols = triangle[rows - 1].length
11+
const dp: number[][] = Array.from({ length: rows }, () => Array(cols).fill(-10001))
12+
function dfs(row: number, col: number): number {
13+
if (row >= triangle.length) {
14+
return 0
15+
}
16+
if (dp[row][col] !== -10001) {
17+
return dp[row][col]
18+
}
19+
const sum = triangle[row][col] + Math.min(
20+
dfs(row + 1, col),
21+
dfs(row + 1, col + 1)
22+
)
23+
dp[row][col] = sum
24+
return sum
25+
}
26+
return dfs(0, 0)
27+
}
28+
29+
export { minimumTotal }

0 commit comments

Comments
 (0)