Skip to content

Added tasks 120-127 #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions LeetCodeNet.Tests/G0101_0200/S0120_triangle/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace LeetCodeNet.G0101_0200.S0120_triangle {

using Xunit;
using System.Collections.Generic;

public class SolutionTest {
[Fact]
public void MinimumTotal() {
var triangle = new List<IList<int>> {
new List<int> {2},
new List<int> {3, 4},
new List<int> {6, 5, 7},
new List<int> {4, 1, 8, 3}
};
Assert.Equal(11, new Solution().MinimumTotal(triangle));
}

[Fact]
public void MinimumTotal2() {
var triangle = new List<IList<int>> {
new List<int> {-10}
};
Assert.Equal(-10, new Solution().MinimumTotal(triangle));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0101_0200.S0122_best_time_to_buy_and_sell_stock_ii {

using Xunit;

public class SolutionTest {
[Fact]
public void MaxProfit() {
Assert.Equal(7, new Solution().MaxProfit(new int[] {7, 1, 5, 3, 6, 4}));
}

[Fact]
public void MaxProfit2() {
Assert.Equal(4, new Solution().MaxProfit(new int[] {1, 2, 3, 4, 5}));
}

[Fact]
public void MaxProfit3() {
Assert.Equal(0, new Solution().MaxProfit(new int[] {7, 6, 4, 3, 1}));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0101_0200.S0123_best_time_to_buy_and_sell_stock_iii {

using Xunit;

public class SolutionTest {
[Fact]
public void MaxProfit() {
Assert.Equal(6, new Solution().MaxProfit(new int[] {3, 3, 5, 0, 0, 3, 1, 4}));
}

[Fact]
public void MaxProfit2() {
Assert.Equal(4, new Solution().MaxProfit(new int[] {1, 2, 3, 4, 5}));
}

[Fact]
public void MaxProfit3() {
Assert.Equal(0, new Solution().MaxProfit(new int[] {7, 6, 4, 3, 1}));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace LeetCodeNet.G0101_0200.S0125_valid_palindrome {

using Xunit;

public class SolutionTest {
[Fact]
public void IsPalindrome() {
Assert.True(new Solution().IsPalindrome("A man, a plan, a canal: Panama"));
}

[Fact]
public void IsPalindrome2() {
Assert.False(new Solution().IsPalindrome("race a car"));
}

[Fact]
public void IsPalindrome3() {
Assert.True(new Solution().IsPalindrome(" "));
}
}
}
19 changes: 19 additions & 0 deletions LeetCodeNet.Tests/G0101_0200/S0127_word_ladder/SolutionTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace LeetCodeNet.G0101_0200.S0127_word_ladder {

using Xunit;
using System.Collections.Generic;

public class SolutionTest {
[Fact]
public void LadderLength() {
Assert.Equal(5, new Solution().LadderLength(
"hit", "cog", new List<string> {"hot", "dot", "dog", "lot", "log", "cog"}));
}

[Fact]
public void LadderLength2() {
Assert.Equal(0, new Solution().LadderLength(
"hit", "cog", new List<string> {"hot", "dot", "dog", "lot", "log"}));
}
}
}
38 changes: 38 additions & 0 deletions LeetCodeNet/G0101_0200/S0120_triangle/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace LeetCodeNet.G0101_0200.S0120_triangle {

// #Medium #Array #Dynamic_Programming #Algorithm_I_Day_12_Dynamic_Programming
// #Dynamic_Programming_I_Day_13 #Udemy_Dynamic_Programming #Top_Interview_150_Multidimensional_DP
// #2025_07_10_Time_2_ms_(90.53%)_Space_44.50_MB_(35.80%)

using System.Collections.Generic;

public class Solution {
public int MinimumTotal(IList<IList<int>> triangle) {
if (triangle == null || triangle.Count == 0) {
return 0;
}
int n = triangle.Count;
int[][] dp = new int[n][];
for (int i = 0; i < n; i++) {
dp[i] = new int[triangle[n - 1].Count];
for (int j = 0; j < dp[i].Length; j++) {
dp[i][j] = -10001;
}
}
return Dfs(triangle, dp, 0, 0);
}

private int Dfs(IList<IList<int>> triangle, int[][] dp, int row, int col) {
if (row >= triangle.Count) {
return 0;
}
if (dp[row][col] != -10001) {
return dp[row][col];
}
int sum = triangle[row][col] +
System.Math.Min(Dfs(triangle, dp, row + 1, col), Dfs(triangle, dp, row + 1, col + 1));
dp[row][col] = sum;
return sum;
}
}
}
37 changes: 37 additions & 0 deletions LeetCodeNet/G0101_0200/S0120_triangle/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
120\. Triangle

Medium

Given a `triangle` array, return _the minimum path sum from top to bottom_.

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.

**Example 1:**

**Input:** triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]

**Output:** 11

**Explanation:**

The triangle looks like:
2
3 4
6 5 7
4 1 8 3
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).

**Example 2:**

**Input:** triangle = [[-10]]

**Output:** -10

**Constraints:**

* `1 <= triangle.length <= 200`
* `triangle[0].length == 1`
* `triangle[i].length == triangle[i - 1].length + 1`
* <code>-10<sup>4</sup> <= triangle[i][j] <= 10<sup>4</sup></code>

**Follow up:** Could you do this using only `O(n)` extra space, where `n` is the total number of rows in the triangle?
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace LeetCodeNet.G0101_0200.S0122_best_time_to_buy_and_sell_stock_ii {

// #Medium #Top_Interview_Questions #Array #Dynamic_Programming #Greedy #Dynamic_Programming_I_Day_7
// #Udemy_Arrays #Top_Interview_150_Array/String
// #2025_07_10_Time_0_ms_(100.00%)_Space_43.59_MB_(83.21%)

public class Solution {
public int MaxProfit(int[] prices) {
int max = 0;
for (int i = 1; i < prices.Length; i++) {
if (prices[i] > prices[i - 1]) {
max += prices[i] - prices[i - 1];
}
}
return max;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
122\. Best Time to Buy and Sell Stock II

Medium

You are given an integer array `prices` where `prices[i]` is the price of a given stock on the `ith` day.

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**.

Find and return _the **maximum** profit you can achieve_.

**Example 1:**

**Input:** prices = [7,1,5,3,6,4]

**Output:** 7

**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.

**Example 2:**

**Input:** prices = [1,2,3,4,5]

**Output:** 4

**Explanation:** Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Total profit is 4.

**Example 3:**

**Input:** prices = [7,6,4,3,1]

**Output:** 0

**Explanation:** There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.

**Constraints:**

* <code>1 <= prices.length <= 3 * 10<sup>4</sup></code>
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace LeetCodeNet.G0101_0200.S0123_best_time_to_buy_and_sell_stock_iii {

// #Hard #Array #Dynamic_Programming #Top_Interview_150_Multidimensional_DP
// #2025_07_10_Time_3_ms_(86.18%)_Space_63.38_MB_(69.12%)

public class Solution {
public int MaxProfit(int[] prices) {
if (prices.Length == 0) {
return 0;
}
int fb = int.MinValue;
int sb = int.MinValue;
int fs = 0;
int ss = 0;
foreach (int price in prices) {
fb = System.Math.Max(fb, -price);
fs = System.Math.Max(fs, fb + price);
sb = System.Math.Max(sb, fs - price);
ss = System.Math.Max(ss, sb + price);
}
return ss;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
123\. Best Time to Buy and Sell Stock III

Hard

You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.

Find the maximum profit you can achieve. You may complete **at most two transactions**.

**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

**Example 1:**

**Input:** prices = [3,3,5,0,0,3,1,4]

**Output:** 6

**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.

**Example 2:**

**Input:** prices = [1,2,3,4,5]

**Output:** 4

**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.

**Example 3:**

**Input:** prices = [7,6,4,3,1]

**Output:** 0

**Explanation:** In this case, no transaction is done, i.e. max profit = 0.

**Example 4:**

**Input:** prices = [1]

**Output:** 0

**Constraints:**

* <code>1 <= prices.length <= 10<sup>5</sup></code>
* <code>0 <= prices[i] <= 10<sup>5</sup></code>
47 changes: 47 additions & 0 deletions LeetCodeNet/G0101_0200/S0125_valid_palindrome/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace LeetCodeNet.G0101_0200.S0125_valid_palindrome {

// #Easy #Top_Interview_Questions #String #Two_Pointers #Udemy_Two_Pointers
// #Top_Interview_150_Two_Pointers #2025_07_10_Time_1_ms_(99.79%)_Space_44.52_MB_(97.65%)

public class Solution {
public bool IsPalindrome(string s) {
int i = 0;
int j = s.Length - 1;
bool res = true;
while (res) {
while (i < j && IsNotAlphaNumeric(s[i])) {
i++;
}
while (i < j && IsNotAlphaNumeric(s[j])) {
j--;
}
if (i >= j) {
break;
}
char left = UpperToLower(s[i]);
char right = UpperToLower(s[j]);
if (left != right) {
res = false;
}
i++;
j--;
}
return res;
}

private bool IsNotAlphaNumeric(char c) {
return (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9');
}

private bool IsUpper(char c) {
return c >= 'A' && c <= 'Z';
}

private char UpperToLower(char c) {
if (IsUpper(c)) {
c = (char)(c + 32);
}
return c;
}
}
}
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy