Skip to content

Commit 74a04d8

Browse files
committed
add more solutions
1 parent a90caf9 commit 74a04d8

File tree

207 files changed

+17296
-321
lines changed

Some content is hidden

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

207 files changed

+17296
-321
lines changed

c#/3Sum.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,86 @@
1-
# 3Sum.md
1+
# [15. 3Sum](https://leetcode.com/problems/3sum/)
2+
3+
## Approach 1: Brute Force (Basic)
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(n^3)
9+
// Space Complexity: O(1) (excluding output list)
10+
using System;
11+
using System.Collections.Generic;
12+
using System.Linq;
13+
14+
public class Solution {
15+
public IList<IList<int>> ThreeSum(int[] nums) {
16+
var result = new HashSet<IList<int>>();
17+
int n = nums.Length;
18+
19+
// Iterate through all possible triplets
20+
for (int i = 0; i < n - 2; i++) {
21+
for (int j = i + 1; j < n - 1; j++) {
22+
for (int k = j + 1; k < n; k++) {
23+
if (nums[i] + nums[j] + nums[k] == 0) {
24+
var triplet = new List<int> { nums[i], nums[j], nums[k] };
25+
triplet.Sort(); // Ensure the triplet is in sorted order
26+
result.Add(triplet); // Add triplet to the set to avoid duplicates
27+
}
28+
}
29+
}
30+
}
31+
32+
return result.ToList(); // Convert set to list for the output
33+
}
34+
}
35+
```
36+
37+
## Approach 2: Two Pointers (Optimal)
38+
39+
### Solution
40+
csharp
41+
```csharp
42+
// Time Complexity: O(n^2)
43+
// Space Complexity: O(n) (for sorting or output list)
44+
using System;
45+
using System.Collections.Generic;
46+
using System.Linq;
47+
48+
public class Solution {
49+
public IList<IList<int>> ThreeSum(int[] nums) {
50+
var result = new List<IList<int>>();
51+
Array.Sort(nums); // Sort the array to use two-pointer technique
52+
53+
for (int i = 0; i < nums.Length - 2; i++) {
54+
// Skip duplicates for the first element
55+
if (i > 0 && nums[i] == nums[i - 1]) {
56+
continue;
57+
}
58+
59+
int left = i + 1;
60+
int right = nums.Length - 1;
61+
62+
// Two-pointer approach
63+
while (left < right) {
64+
int sum = nums[i] + nums[left] + nums[right];
65+
if (sum == 0) {
66+
result.Add(new List<int> { nums[i], nums[left], nums[right] });
67+
68+
// Skip duplicates for the second and third elements
69+
while (left < right && nums[left] == nums[left + 1]) left++;
70+
while (left < right && nums[right] == nums[right - 1]) right--;
71+
72+
left++;
73+
right--;
74+
} else if (sum < 0) {
75+
left++; // Increase the sum
76+
} else {
77+
right--; // Decrease the sum
78+
}
79+
}
80+
}
81+
82+
return result;
83+
}
84+
}
85+
```
286

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,100 @@
1-
# best_time_to_buy_and_sell_stock_with_cooldown.md
1+
# 309. [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/)
2+
3+
## Approach 1: Recursive with Memoization
4+
5+
### Solution
6+
c#
7+
// Time Complexity: O(n)
8+
// Space Complexity: O(n)
9+
using System;
10+
11+
public class Solution {
12+
public int MaxProfit(int[] prices) {
13+
int n = prices.Length;
14+
int?[] memo = new int?[n];
15+
return CalculateMaxProfit(prices, 0, memo);
16+
}
17+
18+
private int CalculateMaxProfit(int[] prices, int currentDay, int?[] memo) {
19+
if (currentDay >= prices.Length) {
20+
return 0;
21+
}
22+
if (memo[currentDay] != null) {
23+
return memo[currentDay].Value;
24+
}
25+
26+
int maxProfit = 0;
27+
for (int sellDay = currentDay + 1; sellDay < prices.Length; sellDay++) {
28+
// Calculate the profit for the current transaction
29+
int profit = prices[sellDay] - prices[currentDay] + CalculateMaxProfit(prices, sellDay + 2, memo);
30+
maxProfit = Math.Max(maxProfit, profit);
31+
}
32+
33+
memo[currentDay] = Math.Max(maxProfit, CalculateMaxProfit(prices, currentDay + 1, memo));
34+
return memo[currentDay].Value;
35+
}
36+
}
37+
38+
## Approach 2: Dynamic Programming with State Variables
39+
40+
### Solution
41+
c#
42+
// Time Complexity: O(n)
43+
// Space Complexity: O(n)
44+
using System;
45+
46+
public class Solution {
47+
public int MaxProfit(int[] prices) {
48+
if (prices.Length == 0) {
49+
return 0;
50+
}
51+
int n = prices.Length;
52+
53+
int[] buy = new int[n];
54+
int[] sell = new int[n];
55+
int[] cooldown = new int[n];
56+
57+
buy[0] = -prices[0];
58+
sell[0] = 0;
59+
cooldown[0] = 0;
60+
61+
for (int i = 1; i < n; i++) {
62+
buy[i] = Math.Max(buy[i - 1], cooldown[i - 1] - prices[i]);
63+
sell[i] = Math.Max(sell[i - 1], buy[i - 1] + prices[i]);
64+
cooldown[i] = Math.Max(cooldown[i - 1], sell[i - 1]);
65+
}
66+
67+
return Math.Max(sell[n - 1], cooldown[n - 1]);
68+
}
69+
}
70+
71+
## Approach 3: Optimized Space Dynamic Programming
72+
73+
### Solution
74+
c#
75+
// Time Complexity: O(n)
76+
// Space Complexity: O(1)
77+
using System;
78+
79+
public class Solution {
80+
public int MaxProfit(int[] prices) {
81+
if (prices.Length == 0) {
82+
return 0;
83+
}
84+
85+
int buy = -prices[0];
86+
int sell = 0;
87+
int cooldown = 0;
88+
89+
for (int i = 1; i < prices.Length; i++) {
90+
int newSell = Math.Max(sell, buy + prices[i]);
91+
int newBuy = Math.Max(buy, cooldown - prices[i]);
92+
cooldown = Math.Max(cooldown, sell);
93+
buy = newBuy;
94+
sell = newSell;
95+
}
96+
97+
return Math.Max(sell, cooldown);
98+
}
99+
}
2100

c#/climbing_stairs.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,78 @@
1-
# climbing_stairs.md
1+
# [70. Climbing Stairs](https://leetcode.com/problems/climbing-stairs/)
2+
3+
## Approach 1: Recursion with Memoization
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(n)
9+
// Space Complexity: O(n)
10+
using System.Collections.Generic;
11+
12+
public class Solution {
13+
private Dictionary<int, int> memo = new Dictionary<int, int>();
14+
15+
public int ClimbStairs(int n) {
16+
if (n <= 2) return n;
17+
if (memo.ContainsKey(n)) return memo[n];
18+
19+
// Recurrence relation: f(n) = f(n-1) + f(n-2)
20+
int result = ClimbStairs(n - 1) + ClimbStairs(n - 2);
21+
memo[n] = result; // Store the result in a map to avoid recalculating
22+
return result;
23+
}
24+
}
25+
```
26+
27+
## Approach 2: Dynamic Programming
28+
29+
### Solution
30+
csharp
31+
```csharp
32+
// Time Complexity: O(n)
33+
// Space Complexity: O(n)
34+
public class Solution {
35+
public int ClimbStairs(int n) {
36+
if (n <= 2) return n;
37+
38+
int[] dp = new int[n + 1];
39+
dp[1] = 1;
40+
dp[2] = 2;
41+
42+
// Build the dp array with previous solutions
43+
for (int i = 3; i <= n; i++) {
44+
dp[i] = dp[i - 1] + dp[i - 2];
45+
}
46+
47+
return dp[n]; // The answer is stored in the last element
48+
}
49+
}
50+
```
51+
52+
## Approach 3: Optimized Dynamic Programming
53+
54+
### Solution
55+
csharp
56+
```csharp
57+
// Time Complexity: O(n)
58+
// Space Complexity: O(1)
59+
public class Solution {
60+
public int ClimbStairs(int n) {
61+
if (n <= 2) return n;
62+
63+
int first = 1; // Represents dp[i-2]
64+
int second = 2; // Represents dp[i-1]
65+
int current = 0;
66+
67+
// Use two variables to avoid full dp array and update them iteratively
68+
for (int i = 3; i <= n; i++) {
69+
current = first + second; // Current step number is sum of previous two steps
70+
first = second; // Move second to first
71+
second = current; // Update second to the current
72+
}
73+
74+
return current; // Result is stored in current
75+
}
76+
}
77+
```
278

c#/coin_change_2.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,94 @@
1-
# coin_change_2.md
1+
# 518. [Coin Change II](https://leetcode.com/problems/coin-change-ii/)
2+
3+
## Approach 1: Recursive Approach
4+
5+
### Solution
6+
csharp
7+
```csharp
8+
// Time Complexity: O(2^n), where n is the number of coins
9+
// Space Complexity: O(n)
10+
public class Solution {
11+
public int Change(int amount, int[] coins) {
12+
return CountWays(coins, coins.Length, amount);
13+
}
14+
15+
// Recursive function to count ways to make change
16+
private int CountWays(int[] coins, int numOfCoins, int amount) {
17+
// Base case: If amount is 0, there is 1 solution (to use no coins)
18+
if (amount == 0) return 1;
19+
20+
// If amount is less than 0, there's no solution
21+
if (amount < 0) return 0;
22+
23+
// If there are no coins and amount is more than 0, no solution
24+
if (numOfCoins <= 0 && amount > 0) return 0;
25+
26+
// Count is the sum of solutions including coins[numOfCoins-1]
27+
// and excluding coins[numOfCoins-1]
28+
return CountWays(coins, numOfCoins - 1, amount)
29+
+ CountWays(coins, numOfCoins, amount - coins[numOfCoins - 1]);
30+
}
31+
}
32+
```
33+
34+
## Approach 2: Dynamic Programming (2D Array)
35+
36+
### Solution
37+
csharp
38+
```csharp
39+
// Time Complexity: O(n * m), where n is the number of coins and m is the amount
40+
// Space Complexity: O(n * m)
41+
public class Solution {
42+
public int Change(int amount, int[] coins) {
43+
// dp[i][j] means the number of ways to make change for amount j using first i types of coins
44+
int[,] dp = new int[coins.Length + 1, amount + 1];
45+
46+
// Base case: If amount is 0, then there's exactly one way to make change: select no coin
47+
for (int i = 0; i <= coins.Length; i++) {
48+
dp[i, 0] = 1;
49+
}
50+
51+
// Fill the dp array
52+
for (int i = 1; i <= coins.Length; i++) {
53+
for (int j = 1; j <= amount; j++) {
54+
// If we don't pick the coin, we have dp[i-1][j] possible ways
55+
dp[i, j] = dp[i - 1, j];
56+
57+
// If we pick the coin, we add the ways we can make up the amount with this coin
58+
if (j >= coins[i - 1]) {
59+
dp[i, j] += dp[i, j - coins[i - 1]];
60+
}
61+
}
62+
}
63+
64+
return dp[coins.Length, amount];
65+
}
66+
}
67+
```
68+
69+
## Approach 3: Dynamic Programming (1D Array)
70+
71+
### Solution
72+
csharp
73+
```csharp
74+
// Time Complexity: O(n * m), where n is the number of coins and m is the amount
75+
// Space Complexity: O(m)
76+
public class Solution {
77+
public int Change(int amount, int[] coins) {
78+
// dp[j] means the number of ways to make change for amount j
79+
int[] dp = new int[amount + 1];
80+
dp[0] = 1; // Base case: one way to make zero amount
81+
82+
// Traverse each coin
83+
foreach (int coin in coins) {
84+
// Update dp array by considering the current coin
85+
for (int j = coin; j <= amount; j++) {
86+
dp[j] += dp[j - coin];
87+
}
88+
}
89+
90+
return dp[amount];
91+
}
92+
}
93+
```
294

0 commit comments

Comments
 (0)