Skip to content

Commit 16a1b5e

Browse files
authored
Added tasks 172-191
1 parent edba2c8 commit 16a1b5e

File tree

16 files changed

+388
-0
lines changed

16 files changed

+388
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
298298
|-|-|-|-|-|-
299299
| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/python/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree/Solution0108.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 0 | 100.00
300300
| 0230 |[Kth Smallest Element in a BST](src/main/python/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution0230.py)| 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
301+
| 0173 |[Binary Search Tree Iterator](src/main/python/g0101_0200/s0173_binary_search_tree_iterator/Solution0173.py)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 2 | 92.39
301302

302303
#### Day 10 Graph/BFS/DFS
303304

@@ -377,6 +378,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
377378
| 0136 |[Single Number](src/main/python/g0101_0200/s0136_single_number/Solution0136.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
378379
| 0007 |[Reverse Integer](src/main/python/g0001_0100/s0007_reverse_integer/Solution0007.py)| Medium | Top_Interview_Questions, Math, Big_O_Time_O(log10(x))_Space_O(1) | 37 | 69.51
379380
| 0009 |[Palindrome Number](src/main/python/g0001_0100/s0009_palindrome_number/Solution0009.py)| Easy | Math, Big_O_Time_O(log10(x))_Space_O(1) | 4 | 80.28
381+
| 0172 |[Factorial Trailing Zeroes](src/main/python/g0101_0200/s0172_factorial_trailing_zeroes/Solution0172.py)| Medium | Top_Interview_Questions, Math | 0 | 100.00
380382

381383
#### Udemy Strings
382384

@@ -521,6 +523,8 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
521523

522524
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
523525
|-|-|-|-|-|-
526+
| 0191 |[Number of 1 Bits](src/main/python/g0101_0200/s0191_number_of_1_bits/Solution0191.py)| Easy | Top_Interview_Questions, Bit_Manipulation | 0 | 100.00
527+
| 0190 |[Reverse Bits](src/main/python/g0101_0200/s0190_reverse_bits/Solution0190.py)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 36 | 75.17
524528
| 0338 |[Counting Bits](src/main/python/g0301_0400/s0338_counting_bits/Solution0338.py)| Easy | Dynamic_Programming, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 3 | 94.51
525529

526530
#### Udemy Design
@@ -639,6 +643,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
639643
| 0112 |[Path Sum](src/main/python/g0101_0200/s0112_path_sum/Solution0112.py)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
640644
| 0129 |[Sum Root to Leaf Numbers](src/main/python/g0101_0200/s0129_sum_root_to_leaf_numbers/Solution0129.py)| Medium | Depth_First_Search, Tree, Binary_Tree | 0 | 100.00
641645
| 0124 |[Binary Tree Maximum Path Sum](src/main/python/g0101_0200/s0124_binary_tree_maximum_path_sum/Solution0124.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Depth_First_Search, Tree, Binary_Tree, Big_O_Time_O(N)_Space_O(N) | 11 | 91.40
646+
| 0173 |[Binary Search Tree Iterator](src/main/python/g0101_0200/s0173_binary_search_tree_iterator/Solution0173.py)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 2 | 92.39
642647
| 0236 |[Lowest Common Ancestor of a Binary Tree](src/main/python/g0201_0300/s0236_lowest_common_ancestor_of_a_binary_tree/Solution0236.py)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, LeetCode_75_Binary_Tree/DFS, Big_O_Time_O(n)_Space_O(n) | 41 | 97.74
643648

644649
#### Top Interview 150 Binary Tree BFS
@@ -725,6 +730,8 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
725730
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
726731
|-|-|-|-|-|-
727732
| 0067 |[Add Binary](src/main/python/g0001_0100/s0067_add_binary/Solution0067.py)| Easy | String, Math, Bit_Manipulation, Simulation | 0 | 100.00
733+
| 0190 |[Reverse Bits](src/main/python/g0101_0200/s0190_reverse_bits/Solution0190.py)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 36 | 75.17
734+
| 0191 |[Number of 1 Bits](src/main/python/g0101_0200/s0191_number_of_1_bits/Solution0191.py)| Easy | Top_Interview_Questions, Bit_Manipulation | 0 | 100.00
728735
| 0136 |[Single Number](src/main/python/g0101_0200/s0136_single_number/Solution0136.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
729736
| 0137 |[Single Number II](src/main/python/g0101_0200/s0137_single_number_ii/Solution0137.py)| Medium | Array, Bit_Manipulation | 0 | 100.00
730737

@@ -734,6 +741,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
734741
|-|-|-|-|-|-
735742
| 0009 |[Palindrome Number](src/main/python/g0001_0100/s0009_palindrome_number/Solution0009.py)| Easy | Math, Big_O_Time_O(log10(x))_Space_O(1) | 4 | 80.28
736743
| 0066 |[Plus One](src/main/python/g0001_0100/s0066_plus_one/Solution0066.py)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
744+
| 0172 |[Factorial Trailing Zeroes](src/main/python/g0101_0200/s0172_factorial_trailing_zeroes/Solution0172.py)| Medium | Top_Interview_Questions, Math | 0 | 100.00
737745

738746
#### Top Interview 150 1D DP
739747

@@ -756,6 +764,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
756764
| 0097 |[Interleaving String](src/main/python/g0001_0100/s0097_interleaving_string/Solution0097.py)| Medium | String, Dynamic_Programming | 37 | 90.93
757765
| 0072 |[Edit Distance](src/main/python/g0001_0100/s0072_edit_distance/Solution0072.py)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, LeetCode_75_DP/Multidimensional, Big_O_Time_O(n^2)_Space_O(n2) | 23 | 98.84
758766
| 0123 |[Best Time to Buy and Sell Stock III](src/main/python/g0101_0200/s0123_best_time_to_buy_and_sell_stock_iii/Solution0123.py)| Hard | Array, Dynamic_Programming | 232 | 75.22
767+
| 0188 |[Best Time to Buy and Sell Stock IV](src/main/python/g0101_0200/s0188_best_time_to_buy_and_sell_stock_iv/Solution.java)| Hard | Array, Dynamic_Programming | 1 | 99.73
759768
| 0221 |[Maximal Square](src/main/python/g0201_0300/s0221_maximal_square/Solution0221.py)| Medium | Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 129 | 64.49
760769

761770
### Data Structure I
@@ -956,6 +965,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
956965
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
957966
|-|-|-|-|-|-
958967
| 0230 |[Kth Smallest Element in a BST](src/main/python/g0201_0300/s0230_kth_smallest_element_in_a_bst/Solution0230.py)| 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
968+
| 0173 |[Binary Search Tree Iterator](src/main/python/g0101_0200/s0173_binary_search_tree_iterator/Solution0173.py)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 2 | 92.39
959969

960970
#### Day 18 Tree
961971

@@ -1059,11 +1069,13 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
10591069

10601070
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10611071
|-|-|-|-|-|-
1072+
| 0191 |[Number of 1 Bits](src/main/python/g0101_0200/s0191_number_of_1_bits/Solution0191.py)| Easy | Top_Interview_Questions, Bit_Manipulation | 0 | 100.00
10621073

10631074
#### Day 14 Bit Manipulation
10641075

10651076
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10661077
|-|-|-|-|-|-
1078+
| 0190 |[Reverse Bits](src/main/python/g0101_0200/s0190_reverse_bits/Solution0190.py)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer | 36 | 75.17
10671079
| 0136 |[Single Number](src/main/python/g0101_0200/s0136_single_number/Solution0136.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, LeetCode_75_Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
10681080

10691081
### Algorithm II
@@ -1512,6 +1524,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
15121524

15131525
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15141526
|-|-|-|-|-|-
1527+
| 0191 |[Number of 1 Bits](src/main/python/g0101_0200/s0191_number_of_1_bits/Solution0191.py)| Easy | Top_Interview_Questions, Bit_Manipulation | 0 | 100.00
15151528

15161529
#### Day 3 Conditional Statements
15171530

@@ -1659,6 +1672,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
16591672
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16601673
|-|-|-|-|-|-
16611674
| 0061 |[Rotate List](src/main/python/g0001_0100/s0061_rotate_list/Solution0061.py)| Medium | Two_Pointers, Linked_List | 0 | 100.00
1675+
| 0173 |[Binary Search Tree Iterator](src/main/python/g0101_0200/s0173_binary_search_tree_iterator/Solution0173.py)| Medium | Tree, Binary_Tree, Stack, Design, Binary_Search_Tree, Iterator | 2 | 92.39
16621676

16631677
#### Day 17
16641678

@@ -1717,7 +1731,12 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
17171731
| 0206 |[Reverse Linked List](src/main/python/g0201_0300/s0206_reverse_linked_list/Solution0206.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, LeetCode_75_LinkedList, 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
17181732
| 0200 |[Number of Islands](src/main/python/g0101_0200/s0200_number_of_islands/Solution0200.py)| 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) | 234 | 82.73
17191733
| 0198 |[House Robber](src/main/python/g0101_0200/s0198_house_robber/Solution0198.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, LeetCode_75_DP/1D, 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
1734+
| 0191 |[Number of 1 Bits](src/main/python/g0101_0200/s0191_number_of_1_bits/Solution0191.py)| 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
1735+
| 0190 |[Reverse Bits](src/main/python/g0101_0200/s0190_reverse_bits/Solution0190.py)| Easy | Top_Interview_Questions, Bit_Manipulation, Divide_and_Conquer, Algorithm_I_Day_14_Bit_Manipulation, Udemy_Bit_Manipulation, Top_Interview_150_Bit_Manipulation | 36 | 75.17
17201736
| 0189 |[Rotate Array](src/main/python/g0101_0200/s0189_rotate_array/Solution0189.py)| 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) | 3 | 74.65
1737+
| 0188 |[Best Time to Buy and Sell Stock IV](src/main/python/g0101_0200/s0188_best_time_to_buy_and_sell_stock_iv/Solution.java)| Hard | Array, Dynamic_Programming, Top_Interview_150_Multidimensional_DP | 1 | 99.73
1738+
| 0173 |[Binary Search Tree Iterator](src/main/python/g0101_0200/s0173_binary_search_tree_iterator/Solution0173.py)| 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 | 2 | 92.39
1739+
| 0172 |[Factorial Trailing Zeroes](src/main/python/g0101_0200/s0172_factorial_trailing_zeroes/Solution0172.py)| Medium | Top_Interview_Questions, Math, Udemy_Integers, Top_Interview_150_Math | 0 | 100.00
17211740
| 0169 |[Majority Element](src/main/python/g0101_0200/s0169_majority_element/Solution0169.py)| 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) | 7 | 56.72
17221741
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/python/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/Solution0167.py)| Medium | Array, Binary_Search, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Binary_Search_I_Day_7, Top_Interview_150_Two_Pointers | 3 | 80.89
17231742
| 0162 |[Find Peak Element](src/main/python/g0101_0200/s0162_find_peak_element/Solution0162.py)| Medium | Top_Interview_Questions, Array, Binary_Search, LeetCode_75_Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_II_Day_12, Top_Interview_150_Binary_Search | 0 | 100.00
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# #Medium #Top_Interview_Questions #Math #Udemy_Integers #Top_Interview_150_Math
2+
# #2025_09_14_Time_0_ms_(100.00%)_Space_17.76_MB_(53.50%)
3+
4+
class Solution:
5+
def trailingZeroes(self, n: int) -> int:
6+
base = 5
7+
count = 0
8+
while n >= base:
9+
count += n // base
10+
base = base * 5
11+
return count
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from Solution0172 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_trailingZeroes(self):
6+
self.assertEqual(Solution().trailingZeroes(3), 0)
7+
8+
def test_trailingZeroes2(self):
9+
self.assertEqual(Solution().trailingZeroes(5), 1)
10+
11+
def test_trailingZeroes3(self):
12+
self.assertEqual(Solution().trailingZeroes(0), 0)
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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_09_14_Time_2_ms_(92.39%)_Space_25.20_MB_(18.38%)
4+
5+
from typing import Optional
6+
7+
class TreeNode:
8+
def __init__(self, val=0, left=None, right=None):
9+
self.val = val
10+
self.left = left
11+
self.right = right
12+
13+
# Definition for a binary tree node.
14+
# class TreeNode:
15+
# def __init__(self, val=0, left=None, right=None):
16+
# self.val = val
17+
# self.left = left
18+
# self.right = right
19+
class BSTIterator:
20+
def __init__(self, root: Optional[TreeNode]):
21+
self.stack = []
22+
self._push_left(root)
23+
24+
def _push_left(self, node):
25+
# Push all left children
26+
while node:
27+
self.stack.append(node)
28+
node = node.left
29+
30+
def next(self) -> int:
31+
# Smallest available node
32+
node = self.stack.pop()
33+
# If node has a right child → push its left subtree
34+
if node.right:
35+
self._push_left(node.right)
36+
return node.val
37+
38+
def hasNext(self) -> bool:
39+
return len(self.stack) > 0
40+
41+
42+
43+
44+
# Your BSTIterator object will be instantiated and called as such:
45+
# obj = BSTIterator(root)
46+
# param_1 = obj.next()
47+
# param_2 = obj.hasNext()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import unittest
2+
from Solution0173 import BSTIterator, TreeNode
3+
4+
class BSTIteratorTest(unittest.TestCase):
5+
def test_iteratorBST(self):
6+
left = TreeNode(3)
7+
right = TreeNode(15)
8+
right.left = TreeNode(9)
9+
right.right = TreeNode(20)
10+
root = TreeNode(7, left, right)
11+
iterator = BSTIterator(root)
12+
self.assertEqual(iterator.next(), 3)
13+
self.assertEqual(iterator.next(), 7)
14+
self.assertTrue(iterator.hasNext())
15+
self.assertEqual(iterator.next(), 9)
16+
self.assertTrue(iterator.hasNext())
17+
self.assertEqual(iterator.next(), 15)
18+
self.assertTrue(iterator.hasNext())
19+
self.assertEqual(iterator.next(), 20)
20+
self.assertFalse(iterator.hasNext())
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g0101_0200.s0188_best_time_to_buy_and_sell_stock_iv;
2+
3+
// #Hard #Array #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
4+
// #2025_03_09_Time_1_ms_(99.73%)_Space_41.76_MB_(82.48%)
5+
6+
public class Solution {
7+
public int maxProfit(int k, int[] prices) {
8+
int n = prices.length;
9+
int[] dp = new int[k + 1];
10+
int[] maxdp = new int[k + 1];
11+
for (int i = 0; i <= k; i++) {
12+
maxdp[i] = Integer.MIN_VALUE;
13+
}
14+
for (int i = 1; i <= n; i++) {
15+
maxdp[0] = Math.max(maxdp[0], dp[0] - prices[i - 1]);
16+
for (int j = k; j >= 1; j--) {
17+
maxdp[j] = Math.max(maxdp[j], dp[j] - prices[i - 1]);
18+
dp[j] = Math.max(maxdp[j - 1] + prices[i - 1], dp[j]);
19+
}
20+
}
21+
return dp[k];
22+
}
23+
}
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_09_14_Time_50_ms_(80.74%)_Space_17.88_MB_(83.54%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def maxProfit(self, k: int, prices: List[int]) -> int:
8+
n = len(prices)
9+
dp = [0] * (k + 1)
10+
maxdp = [float('-inf')] * (k + 1)
11+
12+
for i in range(1, n + 1):
13+
maxdp[0] = max(maxdp[0], dp[0] - prices[i - 1])
14+
for j in range(k, 0, -1):
15+
maxdp[j] = max(maxdp[j], dp[j] - prices[i - 1])
16+
dp[j] = max(maxdp[j - 1] + prices[i - 1], dp[j])
17+
18+
return dp[k]

0 commit comments

Comments
 (0)