You are given three piles of casino chips: white, green and black chips in the form of an array. Each day you need exactly two chips of different colors to play at the casino. You can chose any color, but you are not allowed to use two chips of the same color in a single day.
You will be given an array representing the number of chips of each color and your task is to return the maximum number of days you can play.
Examples:
solve([1,1,1]) = 1, because after you pick on day one, there will be only one chip left
solve([1,2,1] = 2, you can pick twice; you pick two chips on day one then on day two
solve([4,1,1]) = 2
Brute force is not the way to go here. Look for a simplifying mathematical approach.
Tests:
solve([8,1,4])
solve([7,4,10])
solve([12,12,12])
solve([1,23,2])
Good luck!
This challenge comes from KenKamau on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Oldest comments (41)
This should do it
EDIT: first solution does not work for all possible test cases, this should
Doesn't this fail the [12,12,12] input?
The output of
solve([12,12,12])is 12. But you don't have to take my word for it, you can always throw it in the shell and see for yourself.ah I see the correct output should be 18
Yes exactly! I just stumbled upon your solution while looking through the comments for what values I should be outputting, and comparing answers to my code.
Your updated solution works great! I'm not sure I get the "math" side of it, as my solution is definitely brute force (recursively remove tokens until we can't anymore).
I get that your code sorts, then returns either the total number of chips / 2 OR the total number of chips in the smaller stacks, whichever is smaller.
Math-wise, I'll take a stab at understanding it.
If we have 1 larger / infinite stack, then we are limited by the other two. We pair a chip from the smaller stacks with one from the large stack everyday, and that's our total. That's the second half of the return statement.
However, if we have stacks that are somewhat even, we can rotate through and deplete them evenly. We code this by taking the total number of chips, and dividing them by the daily chip requirement (we can use the bitwise operator >> here since the daily chip requirement is 2).
I approached it like this: There's a case where it's better to split the highest stack in half to divvy between the lower 2 stacks, this only happens when the lower 2 stacks add up to be greater than the highest stack, then there are these "leftovers" in the stacks that you can combine.
I ended up with something looking like
high/2 + (mid + low)/2which can be reduced mathematically to
(high + mid + low)/2There was a point where it stopped making sense and I was just reducing the math and logic. This was a solution that helped me dev.to/juanrodriguezarc/comment/12o94
In C with O(1):
JavaScript
code php
function solve(array $arr)
{
$maxDay = 0;
sort($arr);
$checkDuplicateValue = array_unique($arr);
if($arr !== $checkDuplicateValue) {
$maxDay = $arr[0];
return $maxDay;
}
$maxDay = $arr[0] + 1;
return $maxDay;
}
My Javascript solution
Go #GoLang
"constant time" in Javascript
Rust
Look at it go!
Explaination in dev.to/qm3ster/comment/1303g