Skip to content

Commit 2c2f45d

Browse files
committed
Added tasks 120-127
1 parent fa1a2c3 commit 2c2f45d

File tree

6 files changed

+399
-3
lines changed

6 files changed

+399
-3
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 120\. Triangle
5+
6+
Medium
7+
8+
Given a `triangle` array, return _the minimum path sum from top to bottom_.
9+
10+
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.
11+
12+
**Example 1:**
13+
14+
**Input:** triangle = \[\[2],[3,4],[6,5,7],[4,1,8,3]]
15+
16+
**Output:** 11
17+
18+
**Explanation:**
19+
20+
The triangle looks like:
21+
2
22+
3 4
23+
6 5 7
24+
4 1 8 3
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?
41+
42+
## Solution
43+
44+
```csharp
45+
using System.Collections.Generic;
46+
47+
public class Solution {
48+
public int MinimumTotal(IList<IList<int>> triangle) {
49+
if (triangle == null || triangle.Count == 0) {
50+
return 0;
51+
}
52+
int n = triangle.Count;
53+
int[][] dp = new int[n][];
54+
for (int i = 0; i < n; i++) {
55+
dp[i] = new int[triangle[n - 1].Count];
56+
for (int j = 0; j < dp[i].Length; j++) {
57+
dp[i][j] = -10001;
58+
}
59+
}
60+
return Dfs(triangle, dp, 0, 0);
61+
}
62+
63+
private int Dfs(IList<IList<int>> triangle, int[][] dp, int row, int col) {
64+
if (row >= triangle.Count) {
65+
return 0;
66+
}
67+
if (dp[row][col] != -10001) {
68+
return dp[row][col];
69+
}
70+
int sum = triangle[row][col] +
71+
System.Math.Min(Dfs(triangle, dp, row + 1, col), Dfs(triangle, dp, row + 1, col + 1));
72+
dp[row][col] = sum;
73+
return sum;
74+
}
75+
}
76+
```
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 122\. Best Time to Buy and Sell Stock II
5+
6+
Medium
7+
8+
You are given an integer array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
9+
10+
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**.
11+
12+
Find and return _the **maximum** profit you can achieve_.
13+
14+
**Example 1:**
15+
16+
**Input:** prices = [7,1,5,3,6,4]
17+
18+
**Output:** 7
19+
20+
**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.
21+
22+
**Example 2:**
23+
24+
**Input:** prices = [1,2,3,4,5]
25+
26+
**Output:** 4
27+
28+
**Explanation:** Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Total profit is 4.
29+
30+
**Example 3:**
31+
32+
**Input:** prices = [7,6,4,3,1]
33+
34+
**Output:** 0
35+
36+
**Explanation:** There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
37+
38+
**Constraints:**
39+
40+
* <code>1 <= prices.length <= 3 * 10<sup>4</sup></code>
41+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
42+
43+
## Solution
44+
45+
```csharp
46+
public class Solution {
47+
public int MaxProfit(int[] prices) {
48+
int max = 0;
49+
for (int i = 1; i < prices.Length; i++) {
50+
if (prices[i] > prices[i - 1]) {
51+
max += prices[i] - prices[i - 1];
52+
}
53+
}
54+
return max;
55+
}
56+
}
57+
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 123\. Best Time to Buy and Sell Stock III
5+
6+
Hard
7+
8+
You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
9+
10+
Find the maximum profit you can achieve. You may complete **at most two transactions**.
11+
12+
**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
13+
14+
**Example 1:**
15+
16+
**Input:** prices = [3,3,5,0,0,3,1,4]
17+
18+
**Output:** 6
19+
20+
**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.
21+
22+
**Example 2:**
23+
24+
**Input:** prices = [1,2,3,4,5]
25+
26+
**Output:** 4
27+
28+
**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.
29+
30+
**Example 3:**
31+
32+
**Input:** prices = [7,6,4,3,1]
33+
34+
**Output:** 0
35+
36+
**Explanation:** In this case, no transaction is done, i.e. max profit = 0.
37+
38+
**Example 4:**
39+
40+
**Input:** prices = [1]
41+
42+
**Output:** 0
43+
44+
**Constraints:**
45+
46+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
47+
* <code>0 <= prices[i] <= 10<sup>5</sup></code>
48+
49+
## Solution
50+
51+
```csharp
52+
public class Solution {
53+
public int MaxProfit(int[] prices) {
54+
if (prices.Length == 0) {
55+
return 0;
56+
}
57+
int fb = int.MinValue;
58+
int sb = int.MinValue;
59+
int fs = 0;
60+
int ss = 0;
61+
foreach (int price in prices) {
62+
fb = System.Math.Max(fb, -price);
63+
fs = System.Math.Max(fs, fb + price);
64+
sb = System.Math.Max(sb, fs - price);
65+
ss = System.Math.Max(ss, sb + price);
66+
}
67+
return ss;
68+
}
69+
}
70+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 125\. Valid Palindrome
5+
6+
Easy
7+
8+
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.
9+
10+
Given a string `s`, return `true` _if it is a **palindrome**, or_ `false` _otherwise_.
11+
12+
**Example 1:**
13+
14+
**Input:** s = "A man, a plan, a canal: Panama"
15+
16+
**Output:** true
17+
18+
**Explanation:** "amanaplanacanalpanama" is a palindrome.
19+
20+
**Example 2:**
21+
22+
**Input:** s = "race a car"
23+
24+
**Output:** false
25+
26+
**Explanation:** "raceacar" is not a palindrome.
27+
28+
**Example 3:**
29+
30+
**Input:** s = " "
31+
32+
**Output:** true
33+
34+
**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.
35+
36+
**Constraints:**
37+
38+
* <code>1 <= s.length <= 2 * 10<sup>5</sup></code>
39+
* `s` consists only of printable ASCII characters.
40+
41+
## Solution
42+
43+
```csharp
44+
public class Solution {
45+
public bool IsPalindrome(string s) {
46+
int i = 0;
47+
int j = s.Length - 1;
48+
bool res = true;
49+
while (res) {
50+
while (i < j && IsNotAlphaNumeric(s[i])) {
51+
i++;
52+
}
53+
while (i < j && IsNotAlphaNumeric(s[j])) {
54+
j--;
55+
}
56+
if (i >= j) {
57+
break;
58+
}
59+
char left = UpperToLower(s[i]);
60+
char right = UpperToLower(s[j]);
61+
if (left != right) {
62+
res = false;
63+
}
64+
i++;
65+
j--;
66+
}
67+
return res;
68+
}
69+
70+
private bool IsNotAlphaNumeric(char c) {
71+
return (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9');
72+
}
73+
74+
private bool IsUpper(char c) {
75+
return c >= 'A' && c <= 'Z';
76+
}
77+
78+
private char UpperToLower(char c) {
79+
if (IsUpper(c)) {
80+
c = (char)(c + 32);
81+
}
82+
return c;
83+
}
84+
}
85+
```
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Net/LeetCode-in-Net?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Net/LeetCode-in-Net?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Net/LeetCode-in-Net/fork)
3+
4+
## 127\. Word Ladder
5+
6+
Hard
7+
8+
A **transformation sequence** from word `beginWord` to word `endWord` using a dictionary `wordList` is a sequence of words <code>beginWord -> s<sub>1</sub> -> s<sub>2</sub> -> ... -> s<sub>k</sub></code> such that:
9+
10+
* Every adjacent pair of words differs by a single letter.
11+
* Every <code>s<sub>i</sub></code> for `1 <= i <= k` is in `wordList`. Note that `beginWord` does not need to be in `wordList`.
12+
* <code>s<sub>k</sub> == endWord</code>
13+
14+
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._
15+
16+
**Example 1:**
17+
18+
**Input:** beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
19+
20+
**Output:** 5
21+
22+
**Explanation:** One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
23+
24+
**Example 2:**
25+
26+
**Input:** beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
27+
28+
**Output:** 0
29+
30+
**Explanation:** The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
31+
32+
**Constraints:**
33+
34+
* `1 <= beginWord.length <= 10`
35+
* `endWord.length == beginWord.length`
36+
* `1 <= wordList.length <= 5000`
37+
* `wordList[i].length == beginWord.length`
38+
* `beginWord`, `endWord`, and `wordList[i]` consist of lowercase English letters.
39+
* `beginWord != endWord`
40+
* All the words in `wordList` are **unique**.
41+
42+
## Solution
43+
44+
```csharp
45+
using System.Collections.Generic;
46+
47+
public class Solution {
48+
public int LadderLength(string beginWord, string endWord, IList<string> wordDict) {
49+
var beginSet = new HashSet<string>();
50+
var endSet = new HashSet<string>();
51+
var wordSet = new HashSet<string>(wordDict);
52+
var visited = new HashSet<string>();
53+
if (!wordDict.Contains(endWord)) {
54+
return 0;
55+
}
56+
int len = 1;
57+
int strLen = beginWord.Length;
58+
beginSet.Add(beginWord);
59+
endSet.Add(endWord);
60+
while (beginSet.Count > 0 && endSet.Count > 0) {
61+
if (beginSet.Count > endSet.Count) {
62+
var temp = beginSet;
63+
beginSet = endSet;
64+
endSet = temp;
65+
}
66+
var tempSet = new HashSet<string>();
67+
foreach (var s in beginSet) {
68+
char[] chars = s.ToCharArray();
69+
for (int i = 0; i < strLen; i++) {
70+
char old = chars[i];
71+
for (char j = 'a'; j <= 'z'; j++) {
72+
chars[i] = j;
73+
string temp = new string(chars);
74+
if (endSet.Contains(temp)) {
75+
return len + 1;
76+
}
77+
if (!visited.Contains(temp) && wordSet.Contains(temp)) {
78+
tempSet.Add(temp);
79+
visited.Add(temp);
80+
}
81+
}
82+
chars[i] = old;
83+
}
84+
}
85+
beginSet = tempSet;
86+
len++;
87+
}
88+
return 0;
89+
}
90+
}
91+
```

0 commit comments

Comments
 (0)