Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit43307f8

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 parentb99e3f5 commit43307f8

File tree

24 files changed

+3826
-32
lines changed

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+
publicclassSolution {
61+
publicintArrangeCoins(intn) {
62+
introw=0;
63+
while (n-row>0) {
64+
row++;
65+
n-=row;
66+
}
67+
returnrow;
68+
}
69+
}
70+
```
71+
5972
::tabs-end
6073

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

173+
```csharp
174+
publicclassSolution {
175+
publicintArrangeCoins(intn) {
176+
intl=1,r=n;
177+
intres=0;
178+
179+
while (l<=r) {
180+
intmid=l+ (r-l)/2;
181+
longcoins= (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+
returnres;
192+
}
193+
}
194+
```
195+
160196
::tabs-end
161197

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

301+
```csharp
302+
publicclassSolution {
303+
publicintArrangeCoins(intn) {
304+
if (n<=3) {
305+
returnn==1?1:n-1;
306+
}
307+
308+
intl=1,r= (n/2)+1;
309+
while (l<r) {
310+
intmid= (l+r)/2;
311+
longcoins= (long)mid* (mid+1)/2;
312+
313+
if (coins<=n) {
314+
l=mid+1;
315+
}else {
316+
r=mid;
317+
}
318+
}
319+
320+
returnl-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+
publicclassSolution {
413+
publicintArrangeCoins(intn) {
414+
intmask=1<<15;
415+
introws=0;
416+
417+
while (mask>0) {
418+
rows|=mask;
419+
longcoins= (long)rows* (rows+1)/2;
420+
if (coins>n) {
421+
rows^=mask;
422+
}
423+
mask>>=1;
424+
}
425+
426+
returnrows;
427+
}
428+
}
429+
```
430+
351431
::tabs-end
352432

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

479+
```csharp
480+
publicclassSolution {
481+
publicintArrangeCoins(intn) {
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+
publicclassSolution {
72+
publicintBuyChoco(int[]prices,intmoney) {
73+
intres=-1;
74+
for (inti=0;i<prices.Length;i++) {
75+
for (intj=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+
returnres==-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+
publicclassSolution {
145+
publicintBuyChoco(int[]prices,intmoney) {
146+
Array.Sort(prices);
147+
intbuy=prices[0]+prices[1];
148+
returnbuy>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
classSolution {
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+
publicclassSolution {
250+
publicintBuyChoco(int[]prices,intmoney) {
251+
intmin1=int.MaxValue,min2=int.MaxValue;
252+
253+
foreach (intpinprices) {
254+
if (p<min1) {
255+
min2=min1;
256+
min1=p;
257+
}elseif (p<min2) {
258+
min2=p;
259+
}
260+
}
261+
262+
intleftover=money-min1-min2;
263+
returnleftover>=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+
publicclassSolution {
65+
publicintCountOdds(intlow,inthigh) {
66+
intodd=0;
67+
for (intnum=low;num<=high;num++) {
68+
if ((num&1)==1) {
69+
odd++;
70+
}
71+
}
72+
returnodd;
73+
}
74+
}
75+
```
76+
6377
::tabs-end
6478

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

147+
```csharp
148+
publicclassSolution {
149+
publicintCountOdds(intlow,inthigh) {
150+
intlength=high-low+1;
151+
intcount=length/2;
152+
if ((length%2==1)&& (low%2==1)) {
153+
count++;
154+
}
155+
returncount;
156+
}
157+
}
158+
```
159+
133160
::tabs-end
134161

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

209+
```csharp
210+
publicclassSolution {
211+
publicintCountOdds(intlow,inthigh) {
212+
return ((high+1)>>1)- (low>>1);
213+
}
214+
}
215+
```
216+
182217
::tabs-end
183218

184219
###Time & Space Complexity

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp