Skip to content

Commit 1c50915

Browse files
authored
Sri Hari: Batch-6/Neetcode-All/Added-articles (#4074)
* Batch-6/Neetcode-ALL/Added-articles * Batch-6/Neetcode-ALL/Added-articles
1 parent 525065c commit 1c50915

14 files changed

+1395
-2
lines changed

articles/best-time-to-buy-and-sell-stock-ii.md

+109
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ class Solution {
8585
}
8686
```
8787

88+
```csharp
89+
public class Solution {
90+
public int MaxProfit(int[] prices) {
91+
return Rec(prices, 0, false);
92+
}
93+
94+
private int Rec(int[] prices, int i, bool bought) {
95+
if (i == prices.Length) {
96+
return 0;
97+
}
98+
99+
int res = Rec(prices, i + 1, bought);
100+
101+
if (bought) {
102+
res = Math.Max(res, prices[i] + Rec(prices, i + 1, false));
103+
} else {
104+
res = Math.Max(res, -prices[i] + Rec(prices, i + 1, true));
105+
}
106+
107+
return res;
108+
}
109+
}
110+
```
111+
88112
::tabs-end
89113

90114
### Time & Space Complexity
@@ -211,6 +235,43 @@ class Solution {
211235
}
212236
```
213237

238+
```csharp
239+
public class Solution {
240+
public int MaxProfit(int[] prices) {
241+
int n = prices.Length;
242+
int[,] dp = new int[n, 2];
243+
244+
for (int i = 0; i < n; i++) {
245+
dp[i, 0] = -1;
246+
dp[i, 1] = -1;
247+
}
248+
249+
return Rec(prices, 0, 0, dp);
250+
}
251+
252+
private int Rec(int[] prices, int i, int bought, int[,] dp) {
253+
if (i == prices.Length) {
254+
return 0;
255+
}
256+
257+
if (dp[i, bought] != -1) {
258+
return dp[i, bought];
259+
}
260+
261+
int res = Rec(prices, i + 1, bought, dp);
262+
263+
if (bought == 1) {
264+
res = Math.Max(res, prices[i] + Rec(prices, i + 1, 0, dp));
265+
} else {
266+
res = Math.Max(res, -prices[i] + Rec(prices, i + 1, 1, dp));
267+
}
268+
269+
dp[i, bought] = res;
270+
return res;
271+
}
272+
}
273+
```
274+
214275
::tabs-end
215276

216277
### Time & Space Complexity
@@ -290,6 +351,22 @@ class Solution {
290351
}
291352
```
292353

354+
```csharp
355+
public class Solution {
356+
public int MaxProfit(int[] prices) {
357+
int n = prices.Length;
358+
int[,] dp = new int[n + 1, 2];
359+
360+
for (int i = n - 1; i >= 0; i--) {
361+
dp[i, 0] = Math.Max(dp[i + 1, 0], -prices[i] + dp[i + 1, 1]);
362+
dp[i, 1] = Math.Max(dp[i + 1, 1], prices[i] + dp[i + 1, 0]);
363+
}
364+
365+
return dp[0, 0];
366+
}
367+
}
368+
```
369+
293370
::tabs-end
294371

295372
### Time & Space Complexity
@@ -378,6 +455,24 @@ class Solution {
378455
}
379456
```
380457

458+
```csharp
459+
public class Solution {
460+
public int MaxProfit(int[] prices) {
461+
int nextBuy = 0, nextSell = 0;
462+
int curBuy = 0, curSell = 0;
463+
464+
for (int i = prices.Length - 1; i >= 0; i--) {
465+
curBuy = Math.Max(nextBuy, -prices[i] + nextSell);
466+
curSell = Math.Max(nextSell, prices[i] + nextBuy);
467+
nextBuy = curBuy;
468+
nextSell = curSell;
469+
}
470+
471+
return curBuy;
472+
}
473+
}
474+
```
475+
381476
::tabs-end
382477

383478
### Time & Space Complexity
@@ -450,6 +545,20 @@ class Solution {
450545
}
451546
```
452547

548+
```csharp
549+
public class Solution {
550+
public int MaxProfit(int[] prices) {
551+
int profit = 0;
552+
for (int i = 1; i < prices.Length; i++) {
553+
if (prices[i] > prices[i - 1]) {
554+
profit += prices[i] - prices[i - 1];
555+
}
556+
}
557+
return profit;
558+
}
559+
}
560+
```
561+
453562
::tabs-end
454563

455564
### Time & Space Complexity

articles/boats-to-save-people.md

+59
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ class Solution {
7373
}
7474
```
7575

76+
```csharp
77+
public class Solution {
78+
public int NumRescueBoats(int[] people, int limit) {
79+
Array.Sort(people);
80+
int res = 0, l = 0, r = people.Length - 1;
81+
82+
while (l <= r) {
83+
int remain = limit - people[r];
84+
r--;
85+
res++;
86+
87+
if (l <= r && remain >= people[l]) {
88+
l++;
89+
}
90+
}
91+
92+
return res;
93+
}
94+
}
95+
```
96+
7697
::tabs-end
7798

7899
### Time & Space Complexity
@@ -211,6 +232,44 @@ class Solution {
211232
}
212233
```
213234

235+
```csharp
236+
public class Solution {
237+
public int NumRescueBoats(int[] people, int limit) {
238+
int m = 0;
239+
foreach (int p in people) {
240+
m = Math.Max(m, p);
241+
}
242+
243+
int[] count = new int[m + 1];
244+
foreach (int p in people) {
245+
count[p]++;
246+
}
247+
248+
int idx = 0, iVal = 1;
249+
while (idx < people.Length) {
250+
while (count[iVal] == 0) {
251+
iVal++;
252+
}
253+
people[idx] = iVal;
254+
count[iVal]--;
255+
idx++;
256+
}
257+
258+
int res = 0, l = 0, r = people.Length - 1;
259+
while (l <= r) {
260+
int remain = limit - people[r];
261+
r--;
262+
res++;
263+
if (l <= r && remain >= people[l]) {
264+
l++;
265+
}
266+
}
267+
268+
return res;
269+
}
270+
}
271+
```
272+
214273
::tabs-end
215274

216275
### Time & Space Complexity

articles/concatenation-of-array.md

+29-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Solution:
1515
```java
1616
public class Solution {
1717
public int[] getConcatenation(int[] nums) {
18-
int[] ans=new int[2 * nums.length];
18+
int[] ans = new int[2 * nums.length];
1919
int idx = 0;
2020
for (int i = 0; i < 2; i++) {
2121
for (int num : nums) {
@@ -60,6 +60,21 @@ class Solution {
6060
}
6161
```
6262

63+
```csharp
64+
public class Solution {
65+
public int[] GetConcatenation(int[] nums) {
66+
int[] ans = new int[2 * nums.Length];
67+
int idx = 0;
68+
for (int i = 0; i < 2; i++) {
69+
foreach (int num in nums) {
70+
ans[idx++] = num;
71+
}
72+
}
73+
return ans;
74+
}
75+
}
76+
```
77+
6378
::tabs-end
6479

6580
### Time & Space Complexity
@@ -127,6 +142,19 @@ class Solution {
127142
}
128143
```
129144

145+
```csharp
146+
public class Solution {
147+
public int[] GetConcatenation(int[] nums) {
148+
int n = nums.Length;
149+
int[] ans = new int[2 * n];
150+
for (int i = 0; i < n; i++) {
151+
ans[i] = ans[i + n] = nums[i];
152+
}
153+
return ans;
154+
}
155+
}
156+
```
157+
130158
::tabs-end
131159

132160
### Time & Space Complexity

0 commit comments

Comments
 (0)