Skip to content

Commit 315ad0c

Browse files
authored
Added tasks 172-191
1 parent 584c2f1 commit 315ad0c

File tree

16 files changed

+398
-0
lines changed

16 files changed

+398
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
342342

343343
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
344344
|-|-|-|-|-|-
345+
| 0191 |[Number of 1 Bits](src/main/ts/g0101_0200/s0191_number_of_1_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation | 0 | 100.00
345346

346347
#### Day 3 Conditional Statements
347348

@@ -489,6 +490,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
489490
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
490491
|-|-|-|-|-|-
491492
| 0061 |[Rotate List](src/main/ts/g0001_0100/s0061_rotate_list/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
493+
| 0173 |[Binary Search Tree Iterator](src/main/ts/g0101_0200/s0173_binary_search_tree_iterator/solution.ts)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 6 | 98.16
492494

493495
#### Day 17
494496

@@ -784,6 +786,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
784786
|-|-|-|-|-|-
785787
| 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 | 0 | 100.00
786788
| 0230 |[Kth Smallest Element in a BST](src/main/ts/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
789+
| 0173 |[Binary Search Tree Iterator](src/main/ts/g0101_0200/s0173_binary_search_tree_iterator/solution.ts)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 6 | 98.16
787790

788791
#### Day 10 Graph/BFS/DFS
789792

@@ -862,6 +865,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
862865
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 78.27
863866
| 0007 |[Reverse Integer](src/main/ts/g0001_0100/s0007_reverse_integer/solution.ts)| Medium | Top_Interview_Questions, Math | 53 | 83.14
864867
| 0009 |[Palindrome Number](src/main/ts/g0001_0100/s0009_palindrome_number/solution.ts)| Easy | Math | 3 | 99.14
868+
| 0172 |[Factorial Trailing Zeroes](src/main/ts/g0101_0200/s0172_factorial_trailing_zeroes/solution.ts)| Medium | Top_Interview_Questions, Math | 0 | 100.00
865869
| 0050 |[Pow(x, n)](src/main/ts/g0001_0100/s0050_powx_n/solution.ts)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00
866870

867871
#### Udemy Strings
@@ -1005,6 +1009,8 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10051009

10061010
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10071011
|-|-|-|-|-|-
1012+
| 0191 |[Number of 1 Bits](src/main/ts/g0101_0200/s0191_number_of_1_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation | 0 | 100.00
1013+
| 0190 |[Reverse Bits](src/main/ts/g0101_0200/s0190_reverse_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 34 | 99.74
10081014
| 0338 |[Counting Bits](src/main/ts/g0301_0400/s0338_counting_bits/solution.ts)| Easy | Dynamic_Programming, Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 1 | 89.22
10091015

10101016
#### Udemy Design
@@ -1122,6 +1128,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11221128
| 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
11231129
| 0129 |[Sum Root to Leaf Numbers](src/main/ts/g0101_0200/s0129_sum_root_to_leaf_numbers/solution.ts)| Medium | Depth_First_Search, Tree, Binary_Tree | 0 | 100.00
11241130
| 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
1131+
| 0173 |[Binary Search Tree Iterator](src/main/ts/g0101_0200/s0173_binary_search_tree_iterator/solution.ts)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 6 | 98.16
11251132
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/ts/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(n)_Space_O(n) | 61 | 75.97
11261133

11271134
#### Top Interview 150 Binary Tree BFS
@@ -1207,6 +1214,8 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12071214
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12081215
|-|-|-|-|-|-
12091216
| 0067 |[Add Binary](src/main/ts/g0001_0100/s0067_add_binary/solution.ts)| Easy | String, Math, Bit_Manipulation, Simulation | 0 | 100.00
1217+
| 0190 |[Reverse Bits](src/main/ts/g0101_0200/s0190_reverse_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 34 | 99.74
1218+
| 0191 |[Number of 1 Bits](src/main/ts/g0101_0200/s0191_number_of_1_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation | 0 | 100.00
12101219
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 78.27
12111220
| 0137 |[Single Number II](src/main/ts/g0101_0200/s0137_single_number_ii/solution.ts)| Medium | Array, Bit_Manipulation | 0 | 100.00
12121221

@@ -1216,6 +1225,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12161225
|-|-|-|-|-|-
12171226
| 0009 |[Palindrome Number](src/main/ts/g0001_0100/s0009_palindrome_number/solution.ts)| Easy | Math | 3 | 99.14
12181227
| 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
1228+
| 0172 |[Factorial Trailing Zeroes](src/main/ts/g0101_0200/s0172_factorial_trailing_zeroes/solution.ts)| Medium | Top_Interview_Questions, Math | 0 | 100.00
12191229
| 0069 |[Sqrt(x)](src/main/ts/g0001_0100/s0069_sqrtx/solution.ts)| Hard | Array, String, Simulation | 0 | 100.00
12201230
| 0050 |[Pow(x, n)](src/main/ts/g0001_0100/s0050_powx_n/solution.ts)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00
12211231
| 0149 |[Max Points on a Line](src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution.ts)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry | 9 | 97.18
@@ -1241,6 +1251,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12411251
| 0097 |[Interleaving String](src/main/ts/g0001_0100/s0097_interleaving_string/solution.ts)| Medium | String, Dynamic_Programming | 43 | 97.65
12421252
| 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
12431253
| 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
1254+
| 0188 |[Best Time to Buy and Sell Stock IV](src/main/ts/g0101_0200/s0188_best_time_to_buy_and_sell_stock_iv/solution.ts)| Hard | Array, Dynamic_Programming | 0 | 100.00
12441255
| 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
12451256

12461257
### Data Structure I
@@ -1441,6 +1452,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
14411452
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14421453
|-|-|-|-|-|-
14431454
| 0230 |[Kth Smallest Element in a BST](src/main/ts/g0201_0300/s0230_kth_smallest_element_in_a_bst/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1455+
| 0173 |[Binary Search Tree Iterator](src/main/ts/g0101_0200/s0173_binary_search_tree_iterator/solution.ts)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 6 | 98.16
14441456

14451457
#### Day 18 Tree
14461458

@@ -1544,11 +1556,13 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
15441556

15451557
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15461558
|-|-|-|-|-|-
1559+
| 0191 |[Number of 1 Bits](src/main/ts/g0101_0200/s0191_number_of_1_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation | 0 | 100.00
15471560

15481561
#### Day 14 Bit Manipulation
15491562

15501563
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15511564
|-|-|-|-|-|-
1565+
| 0190 |[Reverse Bits](src/main/ts/g0101_0200/s0190_reverse_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 34 | 99.74
15521566
| 0136 |[Single Number](src/main/ts/g0101_0200/s0136_single_number/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 1 | 78.27
15531567

15541568
### Algorithm II
@@ -1720,7 +1734,12 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
17201734
| 0206 |[Reverse Linked List](src/main/ts/g0201_0300/s0206_reverse_linked_list/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
17211735
| 0200 |[Number of Islands](src/main/ts/g0101_0200/s0200_number_of_islands/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph, Top_Interview_150_Graph_General, Big_O_Time_O(M\*N)_Space_O(M\*N) | 57 | 93.94
17221736
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1737+
| 0191 |[Number of 1 Bits](src/main/ts/g0101_0200/s0191_number_of_1_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation, Algorithm_I_Day_13_Bit_Manipulation, Programming_Skills_I_Day_2_Operator, Udemy_Bit_Manipulation, Top_Interview_150_Bit_Manipulation | 0 | 100.00
1738+
| 0190 |[Reverse Bits](src/main/ts/g0101_0200/s0190_reverse_bits/solution.ts)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Bit_Manipulation, Top_Interview_150_Bit_Manipulation | 34 | 99.74
17231739
| 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, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 1 | 86.17
1740+
| 0188 |[Best Time to Buy and Sell Stock IV](src/main/ts/g0101_0200/s0188_best_time_to_buy_and_sell_stock_iv/solution.ts)| Hard | Array, Dynamic_Programming, Top_Interview_150_Multidimensional_DP | 0 | 100.00
1741+
| 0173 |[Binary Search Tree Iterator](src/main/ts/g0101_0200/s0173_binary_search_tree_iterator/solution.ts)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator, Data_Structure_II_Day_17_Tree, Programming_Skills_II_Day_16, Level_2_Day_9_Binary_Search_Tree, Top_Interview_150_Binary_Tree_General | 6 | 98.16
1742+
| 0172 |[Factorial Trailing Zeroes](src/main/ts/g0101_0200/s0172_factorial_trailing_zeroes/solution.ts)| Medium | Top_Interview_Questions, Math, Udemy_Integers, Top_Interview_150_Math | 0 | 100.00
17241743
| 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, Data_Structure_II_Day_1_Array, Udemy_Famous_Algorithm, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
17251744
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts)| Medium | Array, Binary_Search, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Binary_Search_I_Day_7, Top_Interview_150_Two_Pointers | 0 | 100.00
17261745
| 0162 |[Find Peak Element](src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts)| Medium | Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_II_Day_12, Top_Interview_150_Binary_Search | 0 | 100.00
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
172\. Factorial Trailing Zeroes
2+
3+
Medium
4+
5+
Given an integer `n`, return _the number of trailing zeroes in_ `n!`.
6+
7+
Note that `n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1`.
8+
9+
**Example 1:**
10+
11+
**Input:** n = 3
12+
13+
**Output:** 0
14+
15+
**Explanation:** 3! = 6, no trailing zero.
16+
17+
**Example 2:**
18+
19+
**Input:** n = 5
20+
21+
**Output:** 1
22+
23+
**Explanation:** 5! = 120, one trailing zero.
24+
25+
**Example 3:**
26+
27+
**Input:** n = 0
28+
29+
**Output:** 0
30+
31+
**Constraints:**
32+
33+
* <code>0 <= n <= 10<sup>4</sup></code>
34+
35+
**Follow up:** Could you write a solution that works in logarithmic time complexity?
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// #Medium #Top_Interview_Questions #Math #Udemy_Integers #Top_Interview_150_Math
2+
// #2025_04_09_Time_0_ms_(100.00%)_Space_55.76_MB_(76.26%)
3+
4+
function trailingZeroes(n: number): number {
5+
let base = 5
6+
let count = 0
7+
while (n >= base) {
8+
count += Math.floor(n / base)
9+
base *= 5
10+
}
11+
return count
12+
}
13+
14+
export { trailingZeroes }
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
173\. Binary Search Tree Iterator
2+
3+
Medium
4+
5+
Implement the `BSTIterator` class that represents an iterator over the **[in-order traversal](https://en.wikipedia.org/wiki/Tree_traversal#In-order_(LNR))** of a binary search tree (BST):
6+
7+
* `BSTIterator(TreeNode root)` Initializes an object of the `BSTIterator` class. The `root` of the BST is given as part of the constructor. The pointer should be initialized to a non-existent number smaller than any element in the BST.
8+
* `boolean hasNext()` Returns `true` if there exists a number in the traversal to the right of the pointer, otherwise returns `false`.
9+
* `int next()` Moves the pointer to the right, then returns the number at the pointer.
10+
11+
Notice that by initializing the pointer to a non-existent smallest number, the first call to `next()` will return the smallest element in the BST.
12+
13+
You may assume that `next()` calls will always be valid. That is, there will be at least a next number in the in-order traversal when `next()` is called.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2018/12/25/bst-tree.png)
18+
19+
**Input** ["BSTIterator", "next", "next", "hasNext", "next", "hasNext", "next", "hasNext", "next", "hasNext"] [[[7, 3, 15, null, null, 9, 20]], [], [], [], [], [], [], [], [], []]
20+
21+
**Output:** [null, 3, 7, true, 9, true, 15, true, 20, false]
22+
23+
**Explanation:**
24+
25+
BSTIterator bSTIterator = new BSTIterator([7, 3, 15, null, null, 9, 20]);
26+
bSTIterator.next(); // return 3
27+
bSTIterator.next(); // return 7
28+
bSTIterator.hasNext(); // return True
29+
bSTIterator.next(); // return 9
30+
bSTIterator.hasNext(); // return True
31+
bSTIterator.next(); // return 15
32+
bSTIterator.hasNext(); // return True
33+
bSTIterator.next(); // return 20
34+
bSTIterator.hasNext(); // return False
35+
36+
**Constraints:**
37+
38+
* The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.
39+
* <code>0 <= Node.val <= 10<sup>6</sup></code>
40+
* At most <code>10<sup>5</sup></code> calls will be made to `hasNext`, and `next`.
41+
42+
**Follow up:**
43+
44+
* Could you implement `next()` and `hasNext()` to run in average `O(1)` time and use `O(h)` memory, where `h` is the height of the tree?
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// #Medium #Tree #Binary_Tree #Stack #Design #Binary_Search_Tree #Iterator
2+
// #Data_Structure_II_Day_17_Tree #Programming_Skills_II_Day_16 #Level_2_Day_9_Binary_Search_Tree
3+
// #Top_Interview_150_Binary_Tree_General #2025_04_09_Time_6_ms_(98.16%)_Space_69.76_MB_(29.04%)
4+
5+
import { TreeNode } from "../../com_github_leetcode/treenode";
6+
7+
/**
8+
* Definition for a binary tree node.
9+
* class TreeNode {
10+
* val: number
11+
* left: TreeNode | null
12+
* right: TreeNode | null
13+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
14+
* this.val = (val===undefined ? 0 : val)
15+
* this.left = (left===undefined ? null : left)
16+
* this.right = (right===undefined ? null : right)
17+
* }
18+
* }
19+
*/
20+
class BSTIterator {
21+
private node: TreeNode | null;
22+
23+
constructor(root: TreeNode | null) {
24+
this.node = root;
25+
}
26+
27+
next(): number {
28+
let res = -1;
29+
while (this.node !== null) {
30+
if (this.node.left !== null) {
31+
let rightMost = this.node.left;
32+
while (rightMost.right !== null && rightMost.right !== this.node) {
33+
rightMost = rightMost.right;
34+
}
35+
36+
if (rightMost.right === null) {
37+
rightMost.right = this.node;
38+
this.node = this.node.left;
39+
} else {
40+
rightMost.right = null;
41+
res = this.node.val;
42+
this.node = this.node.right;
43+
return res;
44+
}
45+
} else {
46+
res = this.node.val;
47+
this.node = this.node.right;
48+
return res;
49+
}
50+
}
51+
return res;
52+
}
53+
54+
hasNext(): boolean {
55+
return this.node !== null;
56+
}
57+
}
58+
59+
/**
60+
* Your BSTIterator object will be instantiated and called as such:
61+
* var obj = new BSTIterator(root)
62+
* var param_1 = obj.next()
63+
* var param_2 = obj.hasNext()
64+
*/
65+
66+
export { BSTIterator }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
188\. Best Time to Buy and Sell Stock IV
2+
3+
Hard
4+
5+
You are given an integer array `prices` where `prices[i]` is the price of a given stock on the <code>i<sup>th</sup></code> day, and an integer `k`.
6+
7+
Find the maximum profit you can achieve. You may complete at most `k` transactions.
8+
9+
**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
10+
11+
**Example 1:**
12+
13+
**Input:** k = 2, prices = [2,4,1]
14+
15+
**Output:** 2
16+
17+
**Explanation:** Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
18+
19+
**Example 2:**
20+
21+
**Input:** k = 2, prices = [3,2,6,5,0,3]
22+
23+
**Output:** 7
24+
25+
**Explanation:** Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
26+
27+
**Constraints:**
28+
29+
* `0 <= k <= 100`
30+
* `0 <= prices.length <= 1000`
31+
* `0 <= prices[i] <= 1000`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// #Hard #Array #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
2+
// #2025_04_09_Time_0_ms_(100.00%)_Space_58.46_MB_(61.74%)
3+
4+
function maxProfit(k: number, prices: number[]): number {
5+
const n = prices.length
6+
const dp: number[] = new Array(k + 1).fill(0)
7+
const maxdp: number[] = new Array(k + 1).fill(Number.MIN_SAFE_INTEGER)
8+
for (let i = 1; i <= n; i++) {
9+
maxdp[0] = Math.max(maxdp[0], dp[0] - prices[i - 1])
10+
for (let j = k; j >= 1; j--) {
11+
maxdp[j] = Math.max(maxdp[j], dp[j] - prices[i - 1])
12+
dp[j] = Math.max(dp[j], maxdp[j - 1] + prices[i - 1])
13+
}
14+
}
15+
return dp[k]
16+
}
17+
18+
export { maxProfit }

0 commit comments

Comments
 (0)