Skip to content

Commit bb5e67e

Browse files
authored
Added tasks 120-127
1 parent 662c0b2 commit bb5e67e

File tree

15 files changed

+480
-0
lines changed

15 files changed

+480
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace LeetCodeNet.G0101_0200.S0120_triangle {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void MinimumTotal() {
9+
var triangle = new List<IList<int>> {
10+
new List<int> {2},
11+
new List<int> {3, 4},
12+
new List<int> {6, 5, 7},
13+
new List<int> {4, 1, 8, 3}
14+
};
15+
Assert.Equal(11, new Solution().MinimumTotal(triangle));
16+
}
17+
18+
[Fact]
19+
public void MinimumTotal2() {
20+
var triangle = new List<IList<int>> {
21+
new List<int> {-10}
22+
};
23+
Assert.Equal(-10, new Solution().MinimumTotal(triangle));
24+
}
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0101_0200.S0122_best_time_to_buy_and_sell_stock_ii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MaxProfit() {
8+
Assert.Equal(7, new Solution().MaxProfit(new int[] {7, 1, 5, 3, 6, 4}));
9+
}
10+
11+
[Fact]
12+
public void MaxProfit2() {
13+
Assert.Equal(4, new Solution().MaxProfit(new int[] {1, 2, 3, 4, 5}));
14+
}
15+
16+
[Fact]
17+
public void MaxProfit3() {
18+
Assert.Equal(0, new Solution().MaxProfit(new int[] {7, 6, 4, 3, 1}));
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0101_0200.S0123_best_time_to_buy_and_sell_stock_iii {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void MaxProfit() {
8+
Assert.Equal(6, new Solution().MaxProfit(new int[] {3, 3, 5, 0, 0, 3, 1, 4}));
9+
}
10+
11+
[Fact]
12+
public void MaxProfit2() {
13+
Assert.Equal(4, new Solution().MaxProfit(new int[] {1, 2, 3, 4, 5}));
14+
}
15+
16+
[Fact]
17+
public void MaxProfit3() {
18+
Assert.Equal(0, new Solution().MaxProfit(new int[] {7, 6, 4, 3, 1}));
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LeetCodeNet.G0101_0200.S0125_valid_palindrome {
2+
3+
using Xunit;
4+
5+
public class SolutionTest {
6+
[Fact]
7+
public void IsPalindrome() {
8+
Assert.True(new Solution().IsPalindrome("A man, a plan, a canal: Panama"));
9+
}
10+
11+
[Fact]
12+
public void IsPalindrome2() {
13+
Assert.False(new Solution().IsPalindrome("race a car"));
14+
}
15+
16+
[Fact]
17+
public void IsPalindrome3() {
18+
Assert.True(new Solution().IsPalindrome(" "));
19+
}
20+
}
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace LeetCodeNet.G0101_0200.S0127_word_ladder {
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
6+
public class SolutionTest {
7+
[Fact]
8+
public void LadderLength() {
9+
Assert.Equal(5, new Solution().LadderLength(
10+
"hit", "cog", new List<string> {"hot", "dot", "dog", "lot", "log", "cog"}));
11+
}
12+
13+
[Fact]
14+
public void LadderLength2() {
15+
Assert.Equal(0, new Solution().LadderLength(
16+
"hit", "cog", new List<string> {"hot", "dot", "dog", "lot", "log"}));
17+
}
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace LeetCodeNet.G0101_0200.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_07_10_Time_2_ms_(90.53%)_Space_44.50_MB_(35.80%)
6+
7+
using System.Collections.Generic;
8+
9+
public class Solution {
10+
public int MinimumTotal(IList<IList<int>> triangle) {
11+
if (triangle == null || triangle.Count == 0) {
12+
return 0;
13+
}
14+
int n = triangle.Count;
15+
int[][] dp = new int[n][];
16+
for (int i = 0; i < n; i++) {
17+
dp[i] = new int[triangle[n - 1].Count];
18+
for (int j = 0; j < dp[i].Length; j++) {
19+
dp[i][j] = -10001;
20+
}
21+
}
22+
return Dfs(triangle, dp, 0, 0);
23+
}
24+
25+
private int Dfs(IList<IList<int>> triangle, int[][] dp, int row, int col) {
26+
if (row >= triangle.Count) {
27+
return 0;
28+
}
29+
if (dp[row][col] != -10001) {
30+
return dp[row][col];
31+
}
32+
int sum = triangle[row][col] +
33+
System.Math.Min(Dfs(triangle, dp, row + 1, col), Dfs(triangle, dp, row + 1, col + 1));
34+
dp[row][col] = sum;
35+
return sum;
36+
}
37+
}
38+
}
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace LeetCodeNet.G0101_0200.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_07_10_Time_0_ms_(100.00%)_Space_43.59_MB_(83.21%)
6+
7+
public class Solution {
8+
public int MaxProfit(int[] prices) {
9+
int max = 0;
10+
for (int i = 1; i < prices.Length; i++) {
11+
if (prices[i] > prices[i - 1]) {
12+
max += prices[i] - prices[i - 1];
13+
}
14+
}
15+
return max;
16+
}
17+
}
18+
}
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace LeetCodeNet.G0101_0200.S0123_best_time_to_buy_and_sell_stock_iii {
2+
3+
// #Hard #Array #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
4+
// #2025_07_10_Time_3_ms_(86.18%)_Space_63.38_MB_(69.12%)
5+
6+
public class Solution {
7+
public int MaxProfit(int[] prices) {
8+
if (prices.Length == 0) {
9+
return 0;
10+
}
11+
int fb = int.MinValue;
12+
int sb = int.MinValue;
13+
int fs = 0;
14+
int ss = 0;
15+
foreach (int price in prices) {
16+
fb = System.Math.Max(fb, -price);
17+
fs = System.Math.Max(fs, fb + price);
18+
sb = System.Math.Max(sb, fs - price);
19+
ss = System.Math.Max(ss, sb + price);
20+
}
21+
return ss;
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)