Skip to content
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
109 changes: 109 additions & 0 deletions articles/best-time-to-buy-and-sell-stock-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ class Solution {
}
```

```csharp
public class Solution {
public int MaxProfit(int[] prices) {
return Rec(prices, 0, false);
}

private int Rec(int[] prices, int i, bool bought) {
if (i == prices.Length) {
return 0;
}

int res = Rec(prices, i + 1, bought);

if (bought) {
res = Math.Max(res, prices[i] + Rec(prices, i + 1, false));
} else {
res = Math.Max(res, -prices[i] + Rec(prices, i + 1, true));
}

return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -211,6 +235,43 @@ class Solution {
}
```

```csharp
public class Solution {
public int MaxProfit(int[] prices) {
int n = prices.Length;
int[,] dp = new int[n, 2];

for (int i = 0; i < n; i++) {
dp[i, 0] = -1;
dp[i, 1] = -1;
}

return Rec(prices, 0, 0, dp);
}

private int Rec(int[] prices, int i, int bought, int[,] dp) {
if (i == prices.Length) {
return 0;
}

if (dp[i, bought] != -1) {
return dp[i, bought];
}

int res = Rec(prices, i + 1, bought, dp);

if (bought == 1) {
res = Math.Max(res, prices[i] + Rec(prices, i + 1, 0, dp));
} else {
res = Math.Max(res, -prices[i] + Rec(prices, i + 1, 1, dp));
}

dp[i, bought] = res;
return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -290,6 +351,22 @@ class Solution {
}
```

```csharp
public class Solution {
public int MaxProfit(int[] prices) {
int n = prices.Length;
int[,] dp = new int[n + 1, 2];

for (int i = n - 1; i >= 0; i--) {
dp[i, 0] = Math.Max(dp[i + 1, 0], -prices[i] + dp[i + 1, 1]);
dp[i, 1] = Math.Max(dp[i + 1, 1], prices[i] + dp[i + 1, 0]);
}

return dp[0, 0];
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -378,6 +455,24 @@ class Solution {
}
```

```csharp
public class Solution {
public int MaxProfit(int[] prices) {
int nextBuy = 0, nextSell = 0;
int curBuy = 0, curSell = 0;

for (int i = prices.Length - 1; i >= 0; i--) {
curBuy = Math.Max(nextBuy, -prices[i] + nextSell);
curSell = Math.Max(nextSell, prices[i] + nextBuy);
nextBuy = curBuy;
nextSell = curSell;
}

return curBuy;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -450,6 +545,20 @@ class Solution {
}
```

```csharp
public class Solution {
public int MaxProfit(int[] prices) {
int profit = 0;
for (int i = 1; i < prices.Length; i++) {
if (prices[i] > prices[i - 1]) {
profit += prices[i] - prices[i - 1];
}
}
return profit;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
59 changes: 59 additions & 0 deletions articles/boats-to-save-people.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ class Solution {
}
```

```csharp
public class Solution {
public int NumRescueBoats(int[] people, int limit) {
Array.Sort(people);
int res = 0, l = 0, r = people.Length - 1;

while (l <= r) {
int remain = limit - people[r];
r--;
res++;

if (l <= r && remain >= people[l]) {
l++;
}
}

return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -211,6 +232,44 @@ class Solution {
}
```

```csharp
public class Solution {
public int NumRescueBoats(int[] people, int limit) {
int m = 0;
foreach (int p in people) {
m = Math.Max(m, p);
}

int[] count = new int[m + 1];
foreach (int p in people) {
count[p]++;
}

int idx = 0, iVal = 1;
while (idx < people.Length) {
while (count[iVal] == 0) {
iVal++;
}
people[idx] = iVal;
count[iVal]--;
idx++;
}

int res = 0, l = 0, r = people.Length - 1;
while (l <= r) {
int remain = limit - people[r];
r--;
res++;
if (l <= r && remain >= people[l]) {
l++;
}
}

return res;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
30 changes: 29 additions & 1 deletion articles/concatenation-of-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Solution:
```java
public class Solution {
public int[] getConcatenation(int[] nums) {
int[] ans=new int[2 * nums.length];
int[] ans = new int[2 * nums.length];
int idx = 0;
for (int i = 0; i < 2; i++) {
for (int num : nums) {
Expand Down Expand Up @@ -60,6 +60,21 @@ class Solution {
}
```

```csharp
public class Solution {
public int[] GetConcatenation(int[] nums) {
int[] ans = new int[2 * nums.Length];
int idx = 0;
for (int i = 0; i < 2; i++) {
foreach (int num in nums) {
ans[idx++] = num;
}
}
return ans;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -127,6 +142,19 @@ class Solution {
}
```

```csharp
public class Solution {
public int[] GetConcatenation(int[] nums) {
int n = nums.Length;
int[] ans = new int[2 * n];
for (int i = 0; i < n; i++) {
ans[i] = ans[i + n] = nums[i];
}
return ans;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
Loading