Problem Statement
Among solutions that have minimum inequity, we want to choose the one that has the fewest pieces. Each whole bar counts as one piece, while each split bar counts as either two or three pieces depending on how it was split. Create a class Equity that contains a method minPieces that takes n, the number of people, and k, the number of candy bars, and returns the smallest number of pieces that we will need in order to divide the candy bars with minimum inequity.
Definition
- Class:
- Equity
- Method:
- minPieces
- Parameters:
- int, int
- Returns:
- int
- Method signature:
- int minPieces(int n, int k)
- (be sure your method is public)
Constraints
- n will be between 1 and 1000 inclusive
- k will be between 1 and 1000 inclusive
Examples
12
11
Returns: 18
Split 2 of the bars into thirds, and 3 of the bars into halves. Give 6 of the people a third of a bar and a half of a bar each. Give the other 6 people a whole bar each. This results in an inequity of 1 - (1/2+1/3) = 1/6 which is the minimum possible inequity. There are 18 pieces: 6 thirds, 6 halves, and 6 wholes.
12
4
Returns: 12
Give each person a third of a bar.
12
1
Returns: 3
The best we can do is to give 3 people a third, and the other 9 nothing.
12
1000
Returns: 1008
13
1000
Returns: 1007
5
1
Returns: 3
1
1
Returns: 1
1
6
Returns: 6
7
6
Returns: 13
13
12
Returns: 19
15
12
Returns: 30
1
1000
Returns: 1000
1000
1
Returns: 3
1000
1000
Returns: 1000
384
385
Returns: 396
450
784
Returns: 1350
265
385
Returns: 530
864
858
Returns: 900
428
772
Returns: 1284
744
869
Returns: 2226
83
137
Returns: 241
415
833
Returns: 866
905
945
Returns: 1385
600
165
Returns: 495
260
482
Returns: 748
870
378
Returns: 870
517
785
Returns: 1091
299
950
Returns: 1476
401
844
Returns: 1306
83
852
Returns: 947
976
341
Returns: 976
224
680
Returns: 768
991
619
Returns: 1732
242
624
Returns: 840
452
203
Returns: 452
138
430
Returns: 606
974
11
Returns: 33
372
955
Returns: 1266
902
895
Returns: 944
63
350
Returns: 399
93
173
Returns: 264
267
393
Returns: 534
444
794
Returns: 1332
189
876
Returns: 1098
994
832
Returns: 1966
241
291
Returns: 664
894
983
Returns: 1962
877
786
Returns: 1423
841
23
Returns: 69
86
17
Returns: 51
894
112
Returns: 336
674
347
Returns: 734
578
225
Returns: 578
345
272
Returns: 690
450
285
Returns: 810
112
355
Returns: 558
973
435
Returns: 973
539
645
Returns: 1520
199
276
Returns: 398
471
432
Returns: 705
246
152
Returns: 420