diff --git a/LeetCodeNet.Tests/G0101_0200/S0120_triangle/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0120_triangle/SolutionTest.cs new file mode 100644 index 0000000..7ff8a90 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0120_triangle/SolutionTest.cs @@ -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> { + new List {2}, + new List {3, 4}, + new List {6, 5, 7}, + new List {4, 1, 8, 3} + }; + Assert.Equal(11, new Solution().MinimumTotal(triangle)); + } + + [Fact] + public void MinimumTotal2() { + var triangle = new List> { + new List {-10} + }; + Assert.Equal(-10, new Solution().MinimumTotal(triangle)); + } + } +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/SolutionTest.cs new file mode 100644 index 0000000..3fd6068 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/SolutionTest.cs @@ -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})); + } + } +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/SolutionTest.cs new file mode 100644 index 0000000..6f34d74 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/SolutionTest.cs @@ -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})); + } + } +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0125_valid_palindrome/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0125_valid_palindrome/SolutionTest.cs new file mode 100644 index 0000000..e7f6a27 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0125_valid_palindrome/SolutionTest.cs @@ -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(" ")); + } + } +} diff --git a/LeetCodeNet.Tests/G0101_0200/S0127_word_ladder/SolutionTest.cs b/LeetCodeNet.Tests/G0101_0200/S0127_word_ladder/SolutionTest.cs new file mode 100644 index 0000000..8dc6ef6 --- /dev/null +++ b/LeetCodeNet.Tests/G0101_0200/S0127_word_ladder/SolutionTest.cs @@ -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 {"hot", "dot", "dog", "lot", "log", "cog"})); + } + + [Fact] + public void LadderLength2() { + Assert.Equal(0, new Solution().LadderLength( + "hit", "cog", new List {"hot", "dot", "dog", "lot", "log"})); + } + } +} diff --git a/LeetCodeNet/G0101_0200/S0120_triangle/Solution.cs b/LeetCodeNet/G0101_0200/S0120_triangle/Solution.cs new file mode 100644 index 0000000..d93095b --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0120_triangle/Solution.cs @@ -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> 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> 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; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0120_triangle/readme.md b/LeetCodeNet/G0101_0200/S0120_triangle/readme.md new file mode 100644 index 0000000..3441abb --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0120_triangle/readme.md @@ -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` +* -104 <= triangle[i][j] <= 104 + +**Follow up:** Could you do this using only `O(n)` extra space, where `n` is the total number of rows in the triangle? \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/Solution.cs b/LeetCodeNet/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/Solution.cs new file mode 100644 index 0000000..a3ab8ee --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/Solution.cs @@ -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; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/readme.md b/LeetCodeNet/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/readme.md new file mode 100644 index 0000000..4b9c0d0 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0122_best_time_to_buy_and_sell_stock_ii/readme.md @@ -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:** + +* 1 <= prices.length <= 3 * 104 +* 0 <= prices[i] <= 104 \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/Solution.cs b/LeetCodeNet/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/Solution.cs new file mode 100644 index 0000000..add5b55 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/Solution.cs @@ -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; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/readme.md b/LeetCodeNet/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/readme.md new file mode 100644 index 0000000..d702747 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0123_best_time_to_buy_and_sell_stock_iii/readme.md @@ -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:** + +* 1 <= prices.length <= 105 +* 0 <= prices[i] <= 105 \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0125_valid_palindrome/Solution.cs b/LeetCodeNet/G0101_0200/S0125_valid_palindrome/Solution.cs new file mode 100644 index 0000000..5bad564 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0125_valid_palindrome/Solution.cs @@ -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; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0125_valid_palindrome/readme.md b/LeetCodeNet/G0101_0200/S0125_valid_palindrome/readme.md new file mode 100644 index 0000000..3623442 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0125_valid_palindrome/readme.md @@ -0,0 +1,36 @@ +125\. Valid Palindrome + +Easy + +A phrase is a **palindrome** if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. + +Given a string `s`, return `true` _if it is a **palindrome**, or_ `false` _otherwise_. + +**Example 1:** + +**Input:** s = "A man, a plan, a canal: Panama" + +**Output:** true + +**Explanation:** "amanaplanacanalpanama" is a palindrome. + +**Example 2:** + +**Input:** s = "race a car" + +**Output:** false + +**Explanation:** "raceacar" is not a palindrome. + +**Example 3:** + +**Input:** s = " " + +**Output:** true + +**Explanation:** s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome. + +**Constraints:** + +* 1 <= s.length <= 2 * 105 +* `s` consists only of printable ASCII characters. \ No newline at end of file diff --git a/LeetCodeNet/G0101_0200/S0127_word_ladder/Solution.cs b/LeetCodeNet/G0101_0200/S0127_word_ladder/Solution.cs new file mode 100644 index 0000000..53404ac --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0127_word_ladder/Solution.cs @@ -0,0 +1,53 @@ +namespace LeetCodeNet.G0101_0200.S0127_word_ladder { + +// #Hard #Top_Interview_Questions #String #Hash_Table #Breadth_First_Search +// #Graph_Theory_I_Day_12_Breadth_First_Search #Top_Interview_150_Graph_BFS +// #2025_07_09_Time_22_ms_(96.00%)_Space_45.97_MB_(83.68%) + +using System.Collections.Generic; + +public class Solution { + public int LadderLength(string beginWord, string endWord, IList wordDict) { + var beginSet = new HashSet(); + var endSet = new HashSet(); + var wordSet = new HashSet(wordDict); + var visited = new HashSet(); + if (!wordDict.Contains(endWord)) { + return 0; + } + int len = 1; + int strLen = beginWord.Length; + beginSet.Add(beginWord); + endSet.Add(endWord); + while (beginSet.Count > 0 && endSet.Count > 0) { + if (beginSet.Count > endSet.Count) { + var temp = beginSet; + beginSet = endSet; + endSet = temp; + } + var tempSet = new HashSet(); + foreach (var s in beginSet) { + char[] chars = s.ToCharArray(); + for (int i = 0; i < strLen; i++) { + char old = chars[i]; + for (char j = 'a'; j <= 'z'; j++) { + chars[i] = j; + string temp = new string(chars); + if (endSet.Contains(temp)) { + return len + 1; + } + if (!visited.Contains(temp) && wordSet.Contains(temp)) { + tempSet.Add(temp); + visited.Add(temp); + } + } + chars[i] = old; + } + } + beginSet = tempSet; + len++; + } + return 0; + } +} +} diff --git a/LeetCodeNet/G0101_0200/S0127_word_ladder/readme.md b/LeetCodeNet/G0101_0200/S0127_word_ladder/readme.md new file mode 100644 index 0000000..6ccc177 --- /dev/null +++ b/LeetCodeNet/G0101_0200/S0127_word_ladder/readme.md @@ -0,0 +1,37 @@ +127\. Word Ladder + +Hard + +A **transformation sequence** from word `beginWord` to word `endWord` using a dictionary `wordList` is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that: + +* Every adjacent pair of words differs by a single letter. +* Every si for `1 <= i <= k` is in `wordList`. Note that `beginWord` does not need to be in `wordList`. +* sk == endWord + +Given two words, `beginWord` and `endWord`, and a dictionary `wordList`, return _the **number of words** in the **shortest transformation sequence** from_ `beginWord` _to_ `endWord`_, or_ `0` _if no such sequence exists._ + +**Example 1:** + +**Input:** beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"] + +**Output:** 5 + +**Explanation:** One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long. + +**Example 2:** + +**Input:** beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"] + +**Output:** 0 + +**Explanation:** The endWord "cog" is not in wordList, therefore there is no valid transformation sequence. + +**Constraints:** + +* `1 <= beginWord.length <= 10` +* `endWord.length == beginWord.length` +* `1 <= wordList.length <= 5000` +* `wordList[i].length == beginWord.length` +* `beginWord`, `endWord`, and `wordList[i]` consist of lowercase English letters. +* `beginWord != endWord` +* All the words in `wordList` are **unique**. \ No newline at end of file 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