Skip to content

Commit 5fe2997

Browse files
authored
Added tasks 120-135
1 parent f12c053 commit 5fe2997

File tree

33 files changed

+1098
-0
lines changed

33 files changed

+1098
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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:** The triangle looks like:
16+
17+
<ins>2</ins>
18+
19+
<ins>3</ins> 4
20+
21+
6 <ins>5</ins> 7
22+
23+
4 <ins>1</ins> 8 3
24+
25+
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
26+
27+
**Example 2:**
28+
29+
**Input:** triangle = [[-10]]
30+
31+
**Output:** -10
32+
33+
**Constraints:**
34+
35+
* `1 <= triangle.length <= 200`
36+
* `triangle[0].length == 1`
37+
* `triangle[i].length == triangle[i - 1].length + 1`
38+
* <code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code>
39+
40+
**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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package s0120_triangle
2+
3+
// #Medium #Array #Dynamic_Programming #Algorithm_I_Day_12_Dynamic_Programming
4+
// #Dynamic_Programming_I_Day_13 #Udemy_Dynamic_Programming #Top_Interview_150_Multidimensional_DP
5+
// #2025_05_18_Time_0_ms_(100.00%)_Space_5.48_MB_(16.19%)
6+
7+
func minimumTotal(triangle [][]int) int {
8+
if len(triangle) == 0 {
9+
return 0
10+
}
11+
dp := make([][]int, len(triangle))
12+
for i := range dp {
13+
dp[i] = make([]int, len(triangle[len(triangle)-1]))
14+
for j := range dp[i] {
15+
dp[i][j] = -10001
16+
}
17+
}
18+
return dfs(triangle, dp, 0, 0)
19+
}
20+
21+
func dfs(triangle [][]int, dp [][]int, row, col int) int {
22+
if row >= len(triangle) {
23+
return 0
24+
}
25+
if dp[row][col] != -10001 {
26+
return dp[row][col]
27+
}
28+
sum := triangle[row][col] + min(dfs(triangle, dp, row+1, col), dfs(triangle, dp, row+1, col+1))
29+
dp[row][col] = sum
30+
return sum
31+
}
32+
33+
func min(a, b int) int {
34+
if a < b {
35+
return a
36+
}
37+
return b
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package s0120_triangle
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMinimumTotal(t *testing.T) {
9+
triangle := [][]int{
10+
{2},
11+
{3, 4},
12+
{6, 5, 7},
13+
{4, 1, 8, 3},
14+
}
15+
assert.Equal(t, 11, minimumTotal(triangle))
16+
}
17+
18+
func TestMinimumTotal2(t *testing.T) {
19+
triangle := [][]int{
20+
{-10},
21+
}
22+
assert.Equal(t, -10, minimumTotal(triangle))
23+
}
24+
25+
func TestMinimumTotal3(t *testing.T) {
26+
var triangle [][]int
27+
assert.Equal(t, 0, minimumTotal(triangle))
28+
}
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 <code>i<sup>th</sup></code> 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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package s0122_best_time_to_buy_and_sell_stock_ii
2+
3+
// #Medium #Top_Interview_Questions #Array #Dynamic_Programming #Greedy #Dynamic_Programming_I_Day_7
4+
// #Udemy_Arrays #Top_Interview_150_Array/String
5+
// #2025_05_18_Time_0_ms_(100.00%)_Space_5.11_MB_(46.22%)
6+
7+
func maxProfit(prices []int) int {
8+
max := 0
9+
for i := 1; i < len(prices); i++ {
10+
if prices[i] > prices[i-1] {
11+
max += prices[i] - prices[i-1]
12+
}
13+
}
14+
return max
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package s0122_best_time_to_buy_and_sell_stock_ii
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMaxProfit(t *testing.T) {
9+
prices := []int{7, 1, 5, 3, 6, 4}
10+
assert.Equal(t, 7, maxProfit(prices))
11+
}
12+
13+
func TestMaxProfit2(t *testing.T) {
14+
prices := []int{1, 2, 3, 4, 5}
15+
assert.Equal(t, 4, maxProfit(prices))
16+
}
17+
18+
func TestMaxProfit3(t *testing.T) {
19+
prices := []int{7, 6, 4, 3, 1}
20+
assert.Equal(t, 0, maxProfit(prices))
21+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 <code>i<sup>th</sup></code> 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+
**Constraints:**
36+
37+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
38+
* <code>0 <= prices[i] <= 10<sup>5</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package s0123_best_time_to_buy_and_sell_stock_iii
2+
3+
// #Hard #Array #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
4+
// #2025_05_20_Time_0_ms_(100.00%)_Space_12.50_MB_(32.12%)
5+
6+
func maxProfit(prices []int) int {
7+
if len(prices) == 0 {
8+
return 0
9+
}
10+
fb := -1 << 31
11+
sb := -1 << 31
12+
fs := 0
13+
ss := 0
14+
for _, price := range 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+
}
20+
return ss
21+
}
22+
23+
func max(a, b int) int {
24+
if a > b {
25+
return a
26+
}
27+
return b
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package s0123_best_time_to_buy_and_sell_stock_iii
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestMaxProfit(t *testing.T) {
9+
prices := []int{3, 3, 5, 0, 0, 3, 1, 4}
10+
assert.Equal(t, 6, maxProfit(prices))
11+
}
12+
13+
func TestMaxProfit2(t *testing.T) {
14+
prices := []int{1, 2, 3, 4, 5}
15+
assert.Equal(t, 4, maxProfit(prices))
16+
}
17+
18+
func TestMaxProfit3(t *testing.T) {
19+
prices := []int{7, 6, 4, 3, 1}
20+
assert.Equal(t, 0, maxProfit(prices))
21+
}
22+
23+
func TestMaxProfit4(t *testing.T) {
24+
var prices []int
25+
assert.Equal(t, 0, maxProfit(prices))
26+
}

0 commit comments

Comments
 (0)