Skip to content

Commit 2e6563a

Browse files
committed
add more solutions
1 parent 00fbc77 commit 2e6563a

File tree

76 files changed

+6041
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+6041
-76
lines changed
Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,97 @@
1-
# best_time_to_buy_and_sell_stock_3.md
1+
# 123. [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)
2+
3+
## Approach 1: Dynamic Programming with State Variables
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(n)
9+
// Space Complexity: O(1)
10+
public class Solution {
11+
public int MaxProfit(int[] prices) {
12+
int hold1 = int.MinValue, hold2 = int.MinValue;
13+
int release1 = 0, release2 = 0;
14+
15+
foreach (int price in prices) {
16+
// First transaction
17+
release1 = Math.Max(release1, hold1 + price); // Sell stock held by hold1
18+
hold1 = Math.Max(hold1, -price); // Buy stock
19+
20+
// Second transaction
21+
release2 = Math.Max(release2, hold2 + price); // Sell stock held by hold2
22+
hold2 = Math.Max(hold2, release1 - price); // Buy stock after first sell
23+
}
24+
25+
return release2; // Maximum profit from two transactions
26+
}
27+
}
28+
```
29+
30+
## Approach 2: Dynamic Programming with 2D DP Array
31+
32+
### Solution
33+
csharp
34+
```csharp
35+
// Time Complexity: O(n)
36+
// Space Complexity: O(n)
37+
public class Solution {
38+
public int MaxProfit(int[] prices) {
39+
if (prices == null || prices.Length <= 1) return 0;
40+
41+
int k = 2; // Maximum number of transactions
42+
int n = prices.Length;
43+
// dp[i][j] = maximum profit using at most i transactions by time j
44+
int[,] dp = new int[k + 1, n];
45+
46+
for (int i = 1; i <= k; i++) {
47+
int maxDiff = -prices[0];
48+
for (int j = 1; j < n; j++) {
49+
// Previous maximum profit obtained with `i` transactions or profit after selling stock by day `j`
50+
dp[i, j] = Math.Max(dp[i, j - 1], prices[j] + maxDiff);
51+
// Max profit after buying stock on day `j`
52+
maxDiff = Math.Max(maxDiff, dp[i - 1, j] - prices[j]);
53+
}
54+
}
55+
return dp[k, n - 1]; // Maximum profit with at most `k` transactions at the last day
56+
}
57+
}
58+
```
59+
60+
## Approach 3: Forward and Backward Pass
61+
62+
### Solution
63+
csharp
64+
```csharp
65+
// Time Complexity: O(n)
66+
// Space Complexity: O(n)
67+
public class Solution {
68+
public int MaxProfit(int[] prices) {
69+
int n = prices.Length;
70+
if (n <= 1) return 0;
71+
72+
int[] leftProfits = new int[n];
73+
int[] rightProfits = new int[n];
74+
75+
int minPrice = prices[0];
76+
for (int i = 1; i < n; i++) {
77+
minPrice = Math.Min(minPrice, prices[i]);
78+
leftProfits[i] = Math.Max(leftProfits[i - 1], prices[i] - minPrice);
79+
}
80+
81+
int maxPrice = prices[n - 1];
82+
for (int i = n - 2; i >= 0; i--) {
83+
maxPrice = Math.Max(maxPrice, prices[i]);
84+
rightProfits[i] = Math.Max(rightProfits[i + 1], maxPrice - prices[i]);
85+
}
86+
87+
int maxProfit = 0;
88+
for (int i = 0; i < n; i++) {
89+
maxProfit = Math.Max(maxProfit, leftProfits[i] + rightProfits[i]);
90+
}
91+
92+
return maxProfit;
93+
}
94+
}
95+
```
96+
297

c#/clone_graph.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,55 @@
1-
# clone_graph.md
1+
# 133. [Clone Graph](https://leetcode.com/problems/clone-graph/)
2+
3+
## Approach: Depth-First Search (DFS) with Dictionary
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(n), where n is the number of nodes in the graph
9+
// Space Complexity: O(n), for the Dictionary and recursion stack
10+
using System.Collections.Generic;
11+
12+
class Node {
13+
public int val;
14+
public List<Node> neighbors;
15+
public Node() {
16+
val = 0;
17+
neighbors = new List<Node>();
18+
}
19+
public Node(int _val) {
20+
val = _val;
21+
neighbors = new List<Node>();
22+
}
23+
public Node(int _val, List<Node> _neighbors) {
24+
val = _val;
25+
neighbors = _neighbors;
26+
}
27+
}
28+
29+
public class Solution {
30+
private Dictionary<Node, Node> map = new Dictionary<Node, Node>();
31+
32+
public Node CloneGraph(Node node) {
33+
if (node == null) {
34+
return null;
35+
}
36+
37+
// If the node is already cloned, return the cloned node
38+
if (map.ContainsKey(node)) {
39+
return map[node];
40+
}
41+
42+
// Clone the current node
43+
Node clone = new Node(node.val);
44+
map[node] = clone;
45+
46+
// Recursively clone all neighbors
47+
foreach (Node neighbor in node.neighbors) {
48+
clone.neighbors.Add(CloneGraph(neighbor));
49+
}
50+
51+
return clone;
52+
}
53+
}
54+
```
255

c#/design_browser_history.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,61 @@
1-
# design_browser_history.md
1+
# 1472. [Design Browser History](https://leetcode.com/problems/design-browser-history/)
2+
3+
## Approach: Doubly Linked List
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity:
9+
// - Visit: O(1)
10+
// - Back: O(steps)
11+
// - Forward: O(steps)
12+
// Space Complexity: O(n), where n is the number of visited pages
13+
public class BrowserHistory {
14+
private class Node {
15+
public string Url;
16+
public Node Prev, Next;
17+
18+
public Node(string url) {
19+
Url = url;
20+
}
21+
}
22+
23+
private Node current;
24+
25+
public BrowserHistory(string homepage) {
26+
current = new Node(homepage);
27+
}
28+
29+
public void Visit(string url) {
30+
Node newNode = new Node(url);
31+
current.Next = newNode;
32+
newNode.Prev = current;
33+
current = newNode; // Move to the newly visited page
34+
}
35+
36+
public string Back(int steps) {
37+
while (steps > 0 && current.Prev != null) {
38+
current = current.Prev;
39+
steps--;
40+
}
41+
return current.Url;
42+
}
43+
44+
public string Forward(int steps) {
45+
while (steps > 0 && current.Next != null) {
46+
current = current.Next;
47+
steps--;
48+
}
49+
return current.Url;
50+
}
51+
}
52+
53+
/**
54+
* Your BrowserHistory object will be instantiated and called as such:
55+
* BrowserHistory obj = new BrowserHistory(homepage);
56+
* obj.Visit(url);
57+
* string param_2 = obj.Back(steps);
58+
* string param_3 = obj.Forward(steps);
59+
*/
60+
```
261

c#/distinct_subsequences.md

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,123 @@
1-
# distinct_subsequences.md
1+
# 115. [Distinct Subsequences](https://leetcode.com/problems/distinct-subsequences/)
2+
3+
## Approach 1: Recursion with Memoization
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(n * m)
9+
// Space Complexity: O(n * m)
10+
public class Solution {
11+
public int NumDistinct(string s, string t) {
12+
// Memoization table
13+
int[,] memo = new int[s.Length, t.Length];
14+
// Initialize all entries with -1
15+
for (int i = 0; i < s.Length; i++) {
16+
for (int j = 0; j < t.Length; j++) {
17+
memo[i, j] = -1;
18+
}
19+
}
20+
return NumDistinctHelper(s, t, 0, 0, memo);
21+
}
22+
23+
private int NumDistinctHelper(string s, string t, int sIndex, int tIndex, int[,] memo) {
24+
// If t is exhausted, one subsequence is found
25+
if (tIndex == t.Length) {
26+
return 1;
27+
}
28+
// If s is exhausted first, no subsequence can be formed
29+
if (sIndex == s.Length) {
30+
return 0;
31+
}
32+
// Check memoization table
33+
if (memo[sIndex, tIndex] != -1) {
34+
return memo[sIndex, tIndex];
35+
}
36+
37+
// If characters match, both decisions can be made
38+
if (s[sIndex] == t[tIndex]) {
39+
memo[sIndex, tIndex] = NumDistinctHelper(s, t, sIndex + 1, tIndex + 1, memo) +
40+
NumDistinctHelper(s, t, sIndex + 1, tIndex, memo);
41+
} else {
42+
// If characters don't match, skip the current character in s
43+
memo[sIndex, tIndex] = NumDistinctHelper(s, t, sIndex + 1, tIndex, memo);
44+
}
45+
46+
return memo[sIndex, tIndex];
47+
}
48+
}
49+
```
50+
51+
## Approach 2: Dynamic Programming
52+
53+
### Solution
54+
csharp
55+
```csharp
56+
// Time Complexity: O(n * m)
57+
// Space Complexity: O(n * m)
58+
public class Solution {
59+
public int NumDistinct(string s, string t) {
60+
// DP table
61+
int[,] dp = new int[s.Length + 1, t.Length + 1];
62+
63+
// Base case: empty t can be formed by all possible substrings of s
64+
for (int i = 0; i <= s.Length; i++) {
65+
dp[i, t.Length] = 1;
66+
}
67+
68+
// Fill the table in reverse order
69+
for (int i = s.Length - 1; i >= 0; i--) {
70+
for (int j = t.Length - 1; j >= 0; j--) {
71+
if (s[i] == t[j]) {
72+
// Both using the character and ignoring it
73+
dp[i, j] = dp[i + 1, j + 1] + dp[i + 1, j];
74+
} else {
75+
// Ignore the character in s
76+
dp[i, j] = dp[i + 1, j];
77+
}
78+
}
79+
}
80+
81+
// Result is the number of ways to form t[0...m] from s[0...n]
82+
return dp[0, 0];
83+
}
84+
}
85+
```
86+
87+
## Approach 3: Dynamic Programming with Space Optimization
88+
89+
### Solution
90+
csharp
91+
```csharp
92+
// Time Complexity: O(n * m)
93+
// Space Complexity: O(m)
94+
public class Solution {
95+
public int NumDistinct(string s, string t) {
96+
// Previous and current row for space optimization
97+
int[] prev = new int[t.Length + 1];
98+
int[] curr = new int[t.Length + 1];
99+
100+
// Base case: empty t can be formed by all possible substrings of s
101+
for (int i = 0; i <= s.Length; i++) {
102+
prev[t.Length] = 1;
103+
}
104+
105+
// Fill the table in reverse order
106+
for (int i = s.Length - 1; i >= 0; i--) {
107+
for (int j = t.Length - 1; j >= 0; j--) {
108+
if (s[i] == t[j]) {
109+
curr[j] = prev[j + 1] + prev[j];
110+
} else {
111+
curr[j] = prev[j];
112+
}
113+
}
114+
// Move current row to previous for next iteration
115+
Array.Copy(curr, prev, curr.Length);
116+
}
117+
118+
// Result is the number of ways to form t[0...m] from s[0...n]
119+
return prev[0];
120+
}
121+
}
122+
```
2123

0 commit comments

Comments
 (0)