Skip to content

Commit 43307f8

Browse files
authored
Sri Hari: Batch-9/Added articles/Neetcode All (#4689)
* Batch-9/Neetcode-ALL/Added-articles * Batch-9/Neetcode-ALL/Added-articles * Batch-9/Neetcode-ALL/Added-articles * Batch-9/Neetcode-ALL/Added-articles * Batch-9/Neetcode-ALL/Added-articles
1 parent b99e3f5 commit 43307f8

24 files changed

+3826
-32
lines changed

articles/arranging-coins.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ class Solution {
5656
}
5757
```
5858

59+
```csharp
60+
public class Solution {
61+
public int ArrangeCoins(int n) {
62+
int row = 0;
63+
while (n - row > 0) {
64+
row++;
65+
n -= row;
66+
}
67+
return row;
68+
}
69+
}
70+
```
71+
5972
::tabs-end
6073

6174
### Time & Space Complexity
@@ -157,6 +170,29 @@ class Solution {
157170
}
158171
```
159172

173+
```csharp
174+
public class Solution {
175+
public int ArrangeCoins(int n) {
176+
int l = 1, r = n;
177+
int res = 0;
178+
179+
while (l <= r) {
180+
int mid = l + (r - l) / 2;
181+
long coins = (long)mid * (mid + 1) / 2;
182+
183+
if (coins > n) {
184+
r = mid - 1;
185+
} else {
186+
res = Math.Max(res, mid);
187+
l = mid + 1;
188+
}
189+
}
190+
191+
return res;
192+
}
193+
}
194+
```
195+
160196
::tabs-end
161197

162198
### Time & Space Complexity
@@ -262,6 +298,30 @@ class Solution {
262298
}
263299
```
264300

301+
```csharp
302+
public class Solution {
303+
public int ArrangeCoins(int n) {
304+
if (n <= 3) {
305+
return n == 1 ? 1 : n - 1;
306+
}
307+
308+
int l = 1, r = (n / 2) + 1;
309+
while (l < r) {
310+
int mid = (l + r) / 2;
311+
long coins = (long)mid * (mid + 1) / 2;
312+
313+
if (coins <= n) {
314+
l = mid + 1;
315+
} else {
316+
r = mid;
317+
}
318+
}
319+
320+
return l - 1;
321+
}
322+
}
323+
```
324+
265325
::tabs-end
266326

267327
### Time & Space Complexity
@@ -348,6 +408,26 @@ class Solution {
348408
}
349409
```
350410

411+
```csharp
412+
public class Solution {
413+
public int ArrangeCoins(int n) {
414+
int mask = 1 << 15;
415+
int rows = 0;
416+
417+
while (mask > 0) {
418+
rows |= mask;
419+
long coins = (long)rows * (rows + 1) / 2;
420+
if (coins > n) {
421+
rows ^= mask;
422+
}
423+
mask >>= 1;
424+
}
425+
426+
return rows;
427+
}
428+
}
429+
```
430+
351431
::tabs-end
352432

353433
### Time & Space Complexity
@@ -396,6 +476,14 @@ class Solution {
396476
}
397477
```
398478

479+
```csharp
480+
public class Solution {
481+
public int ArrangeCoins(int n) {
482+
return (int)(Math.Sqrt(2L * n + 0.25) - 0.5);
483+
}
484+
}
485+
```
486+
399487
::tabs-end
400488

401489
### Time & Space Complexity

articles/buy-two-chocolates.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public:
4848
4949
```javascript
5050
class Solution {
51+
/**
52+
* @param {number[]} prices
53+
* @param {number} money
54+
* @return {number}
55+
*/
5156
buyChoco(prices, money) {
5257
let res = -1;
5358
for (let i = 0; i < prices.length; i++) {
@@ -62,6 +67,22 @@ class Solution {
6267
}
6368
```
6469

70+
```csharp
71+
public class Solution {
72+
public int BuyChoco(int[] prices, int money) {
73+
int res = -1;
74+
for (int i = 0; i < prices.Length; i++) {
75+
for (int j = i + 1; j < prices.Length; j++) {
76+
if (prices[i] + prices[j] <= money) {
77+
res = Math.Max(res, money - prices[i] - prices[j]);
78+
}
79+
}
80+
}
81+
return res == -1 ? money : res;
82+
}
83+
}
84+
```
85+
6586
::tabs-end
6687

6788
### Time & Space Complexity
@@ -106,6 +127,11 @@ public:
106127
107128
```javascript
108129
class Solution {
130+
/**
131+
* @param {number[]} prices
132+
* @param {number} money
133+
* @return {number}
134+
*/
109135
buyChoco(prices, money) {
110136
prices.sort((a, b) => a - b);
111137
let buy = prices[0] + prices[1];
@@ -114,6 +140,16 @@ class Solution {
114140
}
115141
```
116142

143+
```csharp
144+
public class Solution {
145+
public int BuyChoco(int[] prices, int money) {
146+
Array.Sort(prices);
147+
int buy = prices[0] + prices[1];
148+
return buy > money ? money : money - buy;
149+
}
150+
}
151+
```
152+
117153
::tabs-end
118154

119155
### Time & Space Complexity
@@ -185,6 +221,11 @@ public:
185221

186222
```javascript
187223
class Solution {
224+
/**
225+
* @param {number[]} prices
226+
* @param {number} money
227+
* @return {number}
228+
*/
188229
buyChoco(prices, money) {
189230
let min1 = Infinity,
190231
min2 = Infinity;
@@ -204,6 +245,26 @@ class Solution {
204245
}
205246
```
206247

248+
```csharp
249+
public class Solution {
250+
public int BuyChoco(int[] prices, int money) {
251+
int min1 = int.MaxValue, min2 = int.MaxValue;
252+
253+
foreach (int p in prices) {
254+
if (p < min1) {
255+
min2 = min1;
256+
min1 = p;
257+
} else if (p < min2) {
258+
min2 = p;
259+
}
260+
}
261+
262+
int leftover = money - min1 - min2;
263+
return leftover >= 0 ? leftover : money;
264+
}
265+
}
266+
```
267+
207268
::tabs-end
208269

209270
### Time & Space Complexity

articles/count-odd-numbers-in-an-interval-range.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ class Solution {
6060
}
6161
```
6262

63+
```csharp
64+
public class Solution {
65+
public int CountOdds(int low, int high) {
66+
int odd = 0;
67+
for (int num = low; num <= high; num++) {
68+
if ((num & 1) == 1) {
69+
odd++;
70+
}
71+
}
72+
return odd;
73+
}
74+
}
75+
```
76+
6377
::tabs-end
6478

6579
### Time & Space Complexity
@@ -130,6 +144,19 @@ class Solution {
130144
}
131145
```
132146

147+
```csharp
148+
public class Solution {
149+
public int CountOdds(int low, int high) {
150+
int length = high - low + 1;
151+
int count = length / 2;
152+
if ((length % 2 == 1) && (low % 2 == 1)) {
153+
count++;
154+
}
155+
return count;
156+
}
157+
}
158+
```
159+
133160
::tabs-end
134161

135162
### Time & Space Complexity
@@ -179,6 +206,14 @@ class Solution {
179206
}
180207
```
181208

209+
```csharp
210+
public class Solution {
211+
public int CountOdds(int low, int high) {
212+
return ((high + 1) >> 1) - (low >> 1);
213+
}
214+
}
215+
```
216+
182217
::tabs-end
183218

184219
### Time & Space Complexity

0 commit comments

Comments
 (0)