File tree Expand file tree Collapse file tree 7 files changed +168
-0
lines changed Expand file tree Collapse file tree 7 files changed +168
-0
lines changed Original file line number Diff line number Diff line change @@ -464,6 +464,27 @@ impl Solution {
464
464
}
465
465
}
466
466
```
467
+ ### C#
468
+ ``` csharp
469
+ // 版本二
470
+ public class Solution
471
+ {
472
+ public int Jump (int [] nums )
473
+ {
474
+ int cur = 0 , next = 0 , step = 0 ;
475
+ for (int i = 0 ; i < nums .Length - 1 ; i ++ )
476
+ {
477
+ next = Math .Max (next , i + nums [i ]);
478
+ if (i == cur )
479
+ {
480
+ cur = next ;
481
+ step ++ ;
482
+ }
483
+ }
484
+ return step ;
485
+ }
486
+ }
487
+ ```
467
488
468
489
<p align="center">
469
490
<a href="https://programmercarl.com/other/kstar.html " target="_ blank">
Original file line number Diff line number Diff line change @@ -258,6 +258,23 @@ object Solution {
258
258
}
259
259
}
260
260
```
261
+ ### C#
262
+ ``` csharp
263
+ public class Solution
264
+ {
265
+ public bool CanJump (int [] nums )
266
+ {
267
+ int cover = 0 ;
268
+ if (nums .Length == 1 ) return true ;
269
+ for (int i = 0 ; i <= cover ; i ++ )
270
+ {
271
+ cover = Math .Max (i + nums [i ], cover );
272
+ if (cover >= nums .Length - 1 ) return true ;
273
+ }
274
+ return false ;
275
+ }
276
+ }
277
+ ```
261
278
262
279
<p align =" center " >
263
280
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
Original file line number Diff line number Diff line change @@ -406,6 +406,21 @@ object Solution {
406
406
}
407
407
}
408
408
```
409
+ ### C#
410
+ ``` csharp
411
+ public class Solution
412
+ {
413
+ public int MaxProfit (int [] prices )
414
+ {
415
+ int res = 0 ;
416
+ for (int i = 0 ; i < prices .Length - 1 ; i ++ )
417
+ {
418
+ res += Math .Max (0 , prices [i + 1 ] - prices [i ]);
419
+ }
420
+ return res ;
421
+ }
422
+ }
423
+ ```
409
424
410
425
<p align =" center " >
411
426
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
Original file line number Diff line number Diff line change @@ -630,6 +630,29 @@ object Solution {
630
630
}
631
631
}
632
632
```
633
+ ### C#
634
+ ``` csharp
635
+ // 贪心算法,方法二
636
+ public class Solution
637
+ {
638
+ public int CanCompleteCircuit (int [] gas , int [] cost )
639
+ {
640
+ int curSum = 0 , totalSum = 0 , start = 0 ;
641
+ for (int i = 0 ; i < gas .Length ; i ++ )
642
+ {
643
+ curSum += gas [i ] - cost [i ];
644
+ totalSum += gas [i ] - cost [i ];
645
+ if (curSum < 0 )
646
+ {
647
+ start = i + 1 ;
648
+ curSum = 0 ;
649
+ }
650
+ }
651
+ if (totalSum < 0 ) return - 1 ;
652
+ return start ;
653
+ }
654
+ }
655
+ ```
633
656
634
657
<p align =" center " >
635
658
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
Original file line number Diff line number Diff line change @@ -370,6 +370,35 @@ object Solution {
370
370
}
371
371
}
372
372
```
373
+ ### C#
374
+ ``` csharp
375
+ public class Solution
376
+ {
377
+ public int Candy (int [] ratings )
378
+ {
379
+ int [] candies = new int [ratings .Length ];
380
+ for (int i = 0 ; i < candies .Length ; i ++ )
381
+ {
382
+ candies [i ] = 1 ;
383
+ }
384
+ for (int i = 1 ; i < ratings .Length ; i ++ )
385
+ {
386
+ if (ratings [i ] > ratings [i - 1 ])
387
+ {
388
+ candies [i ] = candies [i - 1 ] + 1 ;
389
+ }
390
+ }
391
+ for (int i = ratings .Length - 2 ; i >= 0 ; i -- )
392
+ {
393
+ if (ratings [i ] > ratings [i + 1 ])
394
+ {
395
+ candies [i ] = Math .Max (candies [i ], candies [i + 1 ] + 1 );
396
+ }
397
+ }
398
+ return candies .Sum ();
399
+ }
400
+ }
401
+ ```
373
402
374
403
375
404
<p align =" center " >
Original file line number Diff line number Diff line change @@ -397,6 +397,46 @@ object Solution {
397
397
}
398
398
}
399
399
```
400
+ ### C#
401
+ ``` csharp
402
+ public class Solution
403
+ {
404
+ public bool LemonadeChange (int [] bills )
405
+ {
406
+ int five = 0 , ten = 0 , twenty = 0 ;
407
+ foreach (var bill in bills )
408
+ {
409
+ if (bill == 5 ) five ++ ;
410
+ if (bill == 10 )
411
+ {
412
+ if (five == 0 ) return false ;
413
+ five -- ;
414
+ ten ++ ;
415
+ }
416
+ if (bill == 20 )
417
+ {
418
+ if (ten > 0 && five > 0 )
419
+ {
420
+ ten -- ;
421
+ five -- ;
422
+ twenty ++ ;
423
+ }
424
+ else if (five >= 3 )
425
+ {
426
+ five -= 3 ;
427
+ twenty ++ ;
428
+ }
429
+ else
430
+ {
431
+ return false ;
432
+ }
433
+
434
+ }
435
+ }
436
+ return true ;
437
+ }
438
+ }
439
+ ```
400
440
401
441
402
442
<p align =" center " >
Original file line number Diff line number Diff line change @@ -322,6 +322,29 @@ object Solution {
322
322
}
323
323
```
324
324
325
+ ### C#
326
+ ``` csharp
327
+ public class Solution
328
+ {
329
+ public int LargestSumAfterKNegations (int [] nums , int k )
330
+ {
331
+ int res = 0 ;
332
+ Array .Sort (nums , (a , b ) => Math .Abs (b ) - Math .Abs (a ));
333
+ for (int i = 0 ; i < nums .Length ; i ++ )
334
+ {
335
+ if (nums [i ] < 0 && k > 0 )
336
+ {
337
+ nums [i ] *= - 1 ;
338
+ k -- ;
339
+ }
340
+ }
341
+ if (k % 2 == 1 ) nums [nums .Length - 1 ] *= - 1 ;
342
+ foreach (var item in nums ) res += item ;
343
+ return res ;
344
+ }
345
+ }
346
+ ```
347
+
325
348
326
349
327
350
<p align =" center " >
You can’t perform that action at this time.
0 commit comments