-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve LeetCode #2558 'Take Gifts From the Richest Pile' [Easy].
- Loading branch information
1 parent
6dafaed
commit 602493a
Showing
4 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...LeetCode.Challenges/Problems25xx/N_2558_TakeGiftsFromTheRichestPile/BruteForceSolution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace LeetCode.Challenges.Problems25xx.N_2558_TakeGiftsFromTheRichestPile; | ||
|
||
public static class BruteForceSolution | ||
{ | ||
public static long PickGifts(int[] gifts, int k) | ||
{ | ||
var maxIndex = gifts.Length - 1; | ||
for (var i = 1; i <= k; i++) | ||
{ | ||
Array.Sort(gifts); | ||
gifts[maxIndex] = (int)Math.Sqrt(gifts[maxIndex]); | ||
} | ||
|
||
return gifts.Select(x => (long)x).Sum(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...Code.Challenges/Problems25xx/N_2558_TakeGiftsFromTheRichestPile/_Description.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Take Gifts From the Richest Pile (#2558) | ||
|
||
You are given an integer array `gifts` denoting the number of gifts in various piles | ||
and `k` seconds. Every second, you do the following: | ||
|
||
- Choose the pile with the maximum number of `gifts`. | ||
- If there is more than one pile with the maximum number of `gifts`, choose any. | ||
- Leave behind the floor of the square root of the number of `gifts` in the pile. Take the rest of the `gifts`. | ||
- Return the number of `gifts` remaining after `k` seconds. | ||
|
||
## Examples: | ||
|
||
### Example 1: | ||
|
||
Input: `gifts = [25,64,9,4,100]`, `k = 4` | ||
Output: `29` | ||
|
||
Explanation: | ||
|
||
The gifts are taken in the following way: | ||
|
||
- In the first second, the last pile is chosen and 10 gifts are left behind. | ||
- Then the second pile is chosen and 8 gifts are left behind. | ||
- After that the first pile is chosen and 5 gifts are left behind. | ||
- Finally, the last pile is chosen again and 3 gifts are left behind. | ||
|
||
The final remaining gifts are `[5,8,9,4,3]`, so the total number of gifts remaining is `29`. | ||
|
||
### Example 2: | ||
|
||
Input: `gifts = [1,1,1,1]`, `k = 4` | ||
Output: `4` | ||
|
||
Explanation: | ||
|
||
- In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile. | ||
- That is, you can't take any pile with you. | ||
|
||
So, the total gifts remaining are 4. | ||
|
||
## Constraints | ||
|
||
- `1 <= gifts.length <= 10^3` | ||
- `1 <= gifts[i] <= 10^9` | ||
- `1 <= k <= 10^3` | ||
|
||
## Hints: | ||
|
||
- Hint 0: The final result might be `long`. | ||
- Hint 1: How can you keep track of the largest gifts in the array | ||
- Hint 2: What is an efficient way to find the square root of a number? | ||
- Hint 3: Can you keep adding up the values of the gifts while ensuring they are in a certain order? | ||
- Hint 4 : Can we use a priority queue or heap here? | ||
|
15 changes: 15 additions & 0 deletions
15
...nges.UnitTests/Problems25xx/N_2558_TakeGiftsFromTheRichestPile/BruteForceSolutionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using LeetCode.Challenges.Problems25xx.N_2558_TakeGiftsFromTheRichestPile; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace LeetCode.Challenges.UnitTests.Problems25xx.N_2558_TakeGiftsFromTheRichestPile; | ||
|
||
public class BruteForceSolutionTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(TestData))] | ||
public void GivenGifts_WhenMinPickGifts_ThenResultAsExpected(int[] numbers, int k, long expectedResult) | ||
{ | ||
BruteForceSolution.PickGifts(numbers, k).ShouldBe(expectedResult); | ||
} | ||
} |
151 changes: 151 additions & 0 deletions
151
...LeetCode.Challenges.UnitTests/Problems25xx/N_2558_TakeGiftsFromTheRichestPile/TestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
using System.Collections; | ||
|
||
namespace LeetCode.Challenges.UnitTests.Problems25xx.N_2558_TakeGiftsFromTheRichestPile; | ||
|
||
public class TestData : IEnumerable<object[]> | ||
{ | ||
public IEnumerator<object[]> GetEnumerator() | ||
{ | ||
yield return [new[] { 25, 64, 9, 4, 100 }, 4, 29]; | ||
yield return [new[] { 1, 1, 1, 1 }, 4, 4]; | ||
yield return [new[] { 1, 2, 2, 2, 2, 2, 1 }, 1000, 7]; | ||
yield return [new[] { 11, 41, 66, 63, 14, 39, 70, 58, 12, 71 }, 20, 17]; | ||
yield return | ||
[ | ||
new[] { 64, 43, 32, 42, 9, 25, 73, 29, 56, 41, 27, 71, 62, 57, 67, 34, 8, 71, 2, 12, 52, 1, 31 }, | ||
51, 33 | ||
]; | ||
yield return | ||
[ | ||
new[] { 70, 51, 68, 36, 67, 31, 28, 54, 41, 74, 31, 38, 24, 25, 24, 5, 34, 61, 9, 12, 17, 20, 5, 11, 75 }, | ||
18, 191 | ||
]; | ||
yield return | ||
[ | ||
new[] { 12, 34, 9, 29, 10, 39, 45, 56, 24, 8, 65, 48, 15, 5, 3, 25, 24, 16, 62, 27, 8, 3, 70, 55, 13, 60 }, | ||
3, 591 | ||
]; | ||
yield return | ||
[ | ||
new[] | ||
{ | ||
5, 52, 73, 54, 6, 22, 58, 9, 34, 21, 58, 68, 63, 72, 1, 5, 64, 42, 37, 46, 17, 40, 50, 54, 11, 1, 25, | ||
43, 21, 31, 29, 58, 49, 73, 54, 40, 60, 7, 54, 25 | ||
}, | ||
37, 214 | ||
]; | ||
yield return | ||
[ | ||
new[] | ||
{ | ||
8, 21, 21, 44, 68, 33, 16, 57, 23, 2, 61, 53, 73, 66, 40, 46, 50, 33, 20, 72, 2, 59, 11, 43, 6, 70, 13, | ||
51, 26, 34, 46, 61, 73, 22, 27, 36, 18, 31, 62 | ||
}, | ||
24, 396 | ||
]; | ||
yield return | ||
[ | ||
new[] | ||
{ | ||
925232828, 595526445, 943146889, 256245587, 728689064, 769490237, 950856413, 731077999, 383929674, | ||
128648955, 859457694, 225621659, 896763559, 236896786, 164798506, 166383433, 300306092, 553922680, | ||
512200506, 328631224, 479853364, 316784103, 206860348, 368493347, 221550031, 144977029, 495887926, | ||
513494415, 844318405, 953827097, 175392954, 549799987, 238979706, 819032664, 462196734, 417865833, | ||
297569847, 233995292, 373888686, 696524569, 329582756, 496452856, 728542943, 142162634, 751068206, | ||
464383587, 404574510, 482031181, 439069715, 782136002, 226541503, 959102126, 660448521, 54672789, | ||
274369682, 611122761, 560583959, 799254990, 53916571, 706613041, 517123207, 619111514, 928784895, | ||
50503729, 442954257, 667795621, 641438958, 187006369, 29825977, 16408383, 640977972, 270604954, | ||
958586437, 188928977, 73928151, 878471655, 34185557, 292880012, 356888682, 23988728, 760705119, | ||
321305064, 909803895, 420518661, 110179275, 407124496, 436887779, 791538904, 355104295, 806548710, | ||
220763530, 744738492, 989111628, 905013330, 841148810, 267069910, 176315834, 933147792, 647044281, | ||
476111834, 884112188, 945958782, 413120247, 337230840, 884665256, 485226774, 574343052, 791258565, | ||
158888828, 872588925, 611281188, 104723070, 789522675, 835360161, 303297176, 486000190, 665712953, | ||
906695178, 124514503, 146616314, 844932868, 326093897, 574153130, 266618657, 701227473, 537174459, | ||
366096397, 825557606, 729369742, 440576912, 614815236, 360112046, 825182568, 72599025, 940320345, | ||
390067525, 529074404, 15609579, 120093674, 158082963, 146415396, 713569083, 274853745, 296896103, | ||
946877161, 136460061, 862008061, 979363734, 257029368, 173803791, 374911919, 366312745, 733706499, | ||
538259544, 238065377, 926286909, 811001551, 252169595, 633515157, 48763658, 907439839, 552434215, | ||
717224930, 209285517, 584515530, 524732756, 898004968, 400418447, 186957277, 423773916, 503350145, | ||
532130920, 218227997, 882025135, 480414102, 586611839, 412641525, 754544122, 324341380, 243827448, | ||
129841074, 554047308, 781445364, 496828370, 849633151, 445297057, 666914318, 919338857, 786401392, | ||
292470034, 339384491, 92317966, 104779724, 822151295, 161656896, 691141963, 896342493, 243224244, | ||
531491389, 572666968, 193152300, 63090766, 493602959, 199254456, 565113320, 801945982, 516162016, | ||
139027014, 27902840, 255589786, 200537646, 211519244, 644547109, 33940979, 365630845, 375311500, | ||
833730715, 516559279, 332499533, 846666734, 353488207, 606607983, 360699958, 346881742, 91694730, | ||
324851346, 682354085, 434841046, 830427138, 83746190, 550677257, 511699483, 236321530, 287774140, | ||
283266802, 36029560, 907177283, 392804258, 619002565, 319747223, 960587085, 853405961, 607004126, | ||
274032068, 250606106, 752530901, 329701084, 356811446, 563281531, 872958082, 313864409, 294141608, | ||
944751745, 405695325, 520992195, 120284283, 751509456, 82666994, 447746996, 211665330, 697316414, | ||
188673242, 735139059, 883967190, 728821938, 887678048, 224212231, 294406611, 924484828, 398904320, | ||
698702932, 562868119, 475121591, 693739991, 473477547, 753909684, 664076204, 990892168, 835049445, | ||
440371959, 225922973, 832281790, 581895166, 477360469, 911931582, 367286462, 101601788, 791338661, | ||
232917492, 659265387, 512869240, 332725577, 994516544, 631015157, 809400352, 113114716, 893753093, | ||
982565776, 23809279, 33883840, 76559283, 729431921, 747359598, 750903231, 842883256, 251241296, | ||
594811996, 453881352, 697634748, 344377884, 539505226, 101988655, 97772634, 518265256, 317363988, | ||
773695807, 532034458, 501250702, 946185166, 64074690, 332352589, 582213046, 703960964, 590466127, | ||
929560330, 507990226, 398351655, 666619708, 508964722, 136598019, 713573796, 579436570, 243212044, | ||
682885090, 53108733, 482626004, 608429018, 258141326, 268421049, 636483770, 243435754, 728012036, | ||
37162835, 304440636, 117817894, 414551848, 361746833, 446955563, 371029892, 153077420, 635135573, | ||
412086639, 490655254, 138355595, 445337274, 169774092, 248074609, 40819127, 401219100, 495279179, | ||
890989991, 706265895, 487428773, 122018395, 734360530, 353614074, 278659807, 641593093, 139481150, | ||
785991828, 758438312, 319330930, 38801621, 209814880, 562508989, 313943458, 280648382, 255366808, | ||
87072562, 492580372, 121673580, 570837987, 522972699, 802608301, 344637158, 935304064, 193493566, | ||
328388713, 540412168, 111822915, 3626704, 218420034, 237173437, 389786125, 577810181, 946859903, | ||
929679885, 192334784, 400526887, 864150582, 357232868, 248315159, 332836362, 208291611, 927296765, | ||
687665329, 551911103, 815253708, 965235700, 58729785, 723767707, 406397121, 275544731, 310718355, | ||
415942069, 289250403, 90457712, 672547474, 61889075, 443442, 997801136, 628879282, 882158258, 142224676, | ||
244573579, 954246078, 601709951, 853497, 579113924, 776527840, 136917143, 631452733, 872966506, | ||
670689606, 309389388, 989589177, 710909372, 185121258, 172338946, 627169765, 522247393, 40298131, | ||
704616776, 533496735, 639685644, 951377012, 334176348, 953065545, 79315185, 740698344, 837986907, | ||
526128639, 986001638, 663577197, 687128100, 341628734, 632486829, 376388414, 843317601, 463063637, | ||
101112708, 801911320, 129217182, 736843970, 17086444, 164644882, 572029212, 607029985, 393986823, | ||
861962885, 953917964, 259885673, 26251800, 84984090, 600970154, 110776463, 706879822, 329858220, | ||
457490036, 412388803, 312415323, 895695971, 345719260, 512410998, 63748294, 820034367, 253140288, | ||
293305612, 750791934, 354530934, 444237786, 846682985, 925663544, 350567355, 60308833, 88311313, | ||
83597243, 41963170, 336303057, 923299067, 611769560, 893380427, 144499637, 800631390, 56314755, | ||
945690351, 983446067, 324495349, 634839683, 830590681, 283831291, 948415843, 870967177, 645117780, | ||
912282979, 857780107, 781237983, 895464359, 239433051, 703276213, 554038700, 858359694, 346036313, | ||
961157445, 593540652, 230607001, 884159246, 222260494, 148850282, 661976291, 480669398, 946374419, | ||
404793926, 877165916, 877097828, 762915059, 166827588, 86717162, 283447487, 800164872, 778180095, | ||
777409345, 403231183, 906308191, 352123923, 729127753, 353523791, 314660819, 683004132, 947825691, | ||
466473132, 355441074, 951446577, 870514802, 695242609, 209216594, 675544419, 918615736, 18685006, | ||
853414189, 813753679, 947734520, 863239147, 694587868, 454888764, 155580267, 337913429, 78904393, | ||
141673462, 248276409, 567849662, 138216710, 519072609, 333947516, 628124745, 49686301, 478049607, | ||
768289536, 473779815, 578986053, 157883309, 698255008, 538785881, 918899917, 416544219, 91956969, | ||
654233103, 912088908, 652110768, 255304130, 880707462, 966621690, 491111453, 682844358, 447403591, | ||
587450128, 351331935, 597593547, 241141519, 47295886, 886716470, 958268458, 22485304, 295327702, | ||
657170479, 431017988, 742787518, 155142360, 235382236, 246539956, 689158471, 871786613, 971023851, | ||
912489358, 665514369, 445522903, 926943639, 639329866, 937857423, 540078544, 925675608, 816061701, | ||
188015995, 156709845, 715870916, 54027850, 794892190, 259185772, 994000963, 104567079, 629198223, | ||
4349548, 255965515, 627950940, 123685145, 68273223, 696944949, 44405551, 206175255, 266001867, | ||
165217329, 233283893, 361008797, 436986000, 566639591, 979270875, 438450040, 88031165, 396413788, | ||
632790614, 94593791, 338030498, 691382118, 852854794, 416826694, 131620863, 548533145, 349961748, | ||
55917936, 80343449, 52202738, 315698495, 319688233, 763276392, 38738268, 861087977, 949037867, | ||
182276810, 115542590, 400858831, 378706286, 305522207, 184224405, 487457354, 705064405, 457409078, | ||
809956392, 203543016, 698314358, 791017039, 261855762, 100394502, 131713059, 445790574, 170760556, | ||
534666011, 633368821, 554347208, 487240335, 943176809, 579001369, 745760091, 563675076, 975869724, | ||
191492749, 819171185, 24999, 767410098, 956811701, 100724154, 18724443, 516402174, 165064068, 222188327, | ||
945621370, 352467702, 612798280, 54719602, 91956955, 541488682, 560983610, 274386677, 863001516, | ||
137889492, 279021365, 355634754, 456925691, 127358063, 792120984, 934783113, 421383527, 746038749, | ||
4529195, 786809931, 305843497, 593101806, 995487870, 738667965, 321667053, 446185689, 652881469, | ||
340965564, 531294080, 756688437, 329774097, 57861849, 930765173, 18379938, 529919952, 772757920, | ||
137143531, 131296506, 709282460, 34973238, 225611778, 268605952, 243763179, 128497142, 674331851, | ||
557182073, 239645136, 998071589, 764709134, 204645257, 541430171, 317073902, 465119714, 316411816, | ||
862972427, 905736178, 496498378, 960463122, 774020419, 513504633, 665221614, 799196123, 614618015, | ||
958037059, 449249842, 229505371, 218842502, 727908252, 895889065, 4780962, 757758796, 231905802, | ||
393140677, 795952153, 178286067, 330185384, 844925816, 233385587, 35915225, 598511960, 467219832, | ||
953036404, 438838135, 580711435, 364264061, 228837463, 138633639, 314846777, 93893465, 139217065, | ||
63501734, 355306871, 54021240, 138960434, 702239437, 71672329, 570044010, 118042430, 119285870, | ||
776718499, 50354985, 127858992, 157694803, 404903811, 924255255, 659439695, 327569007, 32652821, | ||
473639077, 109987848, 204716250, 651929448, 780505846, 653545481, 546725473, 603950385, 13229597, | ||
865857114, 250479566, 177003454, 8625597, 614913834, 469869702, 283735705, 935415858, 312204373, | ||
945613425, 639268843, 647212445, 900771602, 194533201, 978618066, 907958078, 548178769, 642657914, | ||
422348586, 969710472, 338667995, 112999243, 179204194, 128158549, 591384487, 155620654, 508783337, | ||
167349320, 10831193, 646513612, 36332014, 318895832, 233277439, 127466023, 278938454 | ||
}, | ||
600, 36685293358 | ||
]; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); | ||
} |