Skip to content

Commit f995bf5

Browse files
authored
Added tasks 120-127
1 parent baaf053 commit f995bf5

File tree

15 files changed

+355
-0
lines changed

15 files changed

+355
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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_09_14_Time_0_ms_(100.00%)_Space_18.71_MB_(81.95%)
4+
5+
from typing import List
6+
7+
class Solution:
8+
def minimumTotal(self, triangle: List[List[int]]) -> int:
9+
# create a 1D array, copy last elements
10+
# iterate reverse last - 1
11+
n = len(triangle)
12+
dp = []
13+
for num in triangle[-1]:
14+
dp.append(num)
15+
for i in range(n-2, -1, -1):
16+
for j in range(i+1):
17+
dp[j] = triangle[i][j] + min(dp[j], dp[j+1])
18+
return dp[0]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import unittest
2+
from Solution0120 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_minimumTotal(self):
6+
triangle = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]
7+
self.assertEqual(Solution().minimumTotal(triangle), 11)
8+
9+
def test_minimumTotal2(self):
10+
triangle = [[-10]]
11+
self.assertEqual(Solution().minimumTotal(triangle), -10)
Lines changed: 37 additions & 0 deletions
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?
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# #Medium #Top_Interview_Questions #Array #Dynamic_Programming #Greedy #Dynamic_Programming_I_Day_7
2+
# #Udemy_Arrays #Top_Interview_150_Array/String
3+
# #2025_09_14_Time_0_ms_(100.00%)_Space_18.77_MB_(70.67%)
4+
5+
from typing import List
6+
7+
class Solution:
8+
def maxProfit(self, prices: List[int]) -> int:
9+
max_profit = 0
10+
for i in range(1, len(prices)):
11+
if prices[i] > prices[i - 1]:
12+
max_profit += prices[i] - prices[i - 1]
13+
return max_profit
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from Solution0122 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_maxProfit(self):
6+
self.assertEqual(Solution().maxProfit([7, 1, 5, 3, 6, 4]), 7)
7+
8+
def test_maxProfit2(self):
9+
self.assertEqual(Solution().maxProfit([1, 2, 3, 4, 5]), 4)
10+
11+
def test_maxProfit3(self):
12+
self.assertEqual(Solution().maxProfit([7, 6, 4, 3, 1]), 0)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
122\. Best Time to Buy and Sell Stock II
2+
3+
Medium
4+
5+
You are given an integer array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
6+
7+
On each day, you may decide to buy and/or sell the stock. You can only hold **at most one** share of the stock at any time. However, you can buy it then immediately sell it on the **same day**.
8+
9+
Find and return _the **maximum** profit you can achieve_.
10+
11+
**Example 1:**
12+
13+
**Input:** prices = [7,1,5,3,6,4]
14+
15+
**Output:** 7
16+
17+
**Explanation:** Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Total profit is 4 + 3 = 7.
18+
19+
**Example 2:**
20+
21+
**Input:** prices = [1,2,3,4,5]
22+
23+
**Output:** 4
24+
25+
**Explanation:** Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Total profit is 4.
26+
27+
**Example 3:**
28+
29+
**Input:** prices = [7,6,4,3,1]
30+
31+
**Output:** 0
32+
33+
**Explanation:** There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= prices.length <= 3 * 10<sup>4</sup></code>
38+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# #Hard #Array #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
2+
# #2025_09_14_Time_232_ms_(75.22%)_Space_28.79_MB_(85.10%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def maxProfit(self, prices: List[int]) -> int:
8+
if len(prices) == 0:
9+
return 0
10+
fb = float('-inf') # first buy
11+
sb = float('-inf') # second buy
12+
fs = 0 # first sell
13+
ss = 0 # second sell
14+
for price in prices:
15+
fb = max(fb, -price)
16+
fs = max(fs, fb + price)
17+
sb = max(sb, fs - price)
18+
ss = max(ss, sb + price)
19+
return ss
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from Solution0123 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_maxProfit(self):
6+
self.assertEqual(Solution().maxProfit([3, 3, 5, 0, 0, 3, 1, 4]), 6)
7+
8+
def test_maxProfit2(self):
9+
self.assertEqual(Solution().maxProfit([1, 2, 3, 4, 5]), 4)
10+
11+
def test_maxProfit3(self):
12+
self.assertEqual(Solution().maxProfit([7, 6, 4, 3, 1]), 0)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
123\. Best Time to Buy and Sell Stock III
2+
3+
Hard
4+
5+
You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
6+
7+
Find the maximum profit you can achieve. You may complete **at most two 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:** prices = [3,3,5,0,0,3,1,4]
14+
15+
**Output:** 6
16+
17+
**Explanation:** Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
18+
19+
**Example 2:**
20+
21+
**Input:** prices = [1,2,3,4,5]
22+
23+
**Output:** 4
24+
25+
**Explanation:** Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
26+
27+
**Example 3:**
28+
29+
**Input:** prices = [7,6,4,3,1]
30+
31+
**Output:** 0
32+
33+
**Explanation:** In this case, no transaction is done, i.e. max profit = 0.
34+
35+
**Example 4:**
36+
37+
**Input:** prices = [1]
38+
39+
**Output:** 0
40+
41+
**Constraints:**
42+
43+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
44+
* <code>0 <= prices[i] <= 10<sup>5</sup></code>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# #Hard #Array #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
2+
# #2025_09_14_Time_232_ms_(75.22%)_Space_28.79_MB_(85.10%)
3+
4+
import re
5+
6+
class Solution:
7+
def isPalindrome(self, s: str) -> bool:
8+
cleaned = ''.join(re.findall('[A-Za-z0-9]+', s.lower()))
9+
return cleaned == cleaned[::-1]

0 commit comments

Comments
 (0)