DEV Community

Discussion on: Daily Challenge #275 - Casino Chips

Collapse
 
masterroshan profile image
TJ Johnson • Edited

This should do it

def solve(chips):
  max_chips = max(chips)
  chips.remove(max_chips)
  remaining_count = sum(chips)
  return min(max_chips, remaining_count)
Enter fullscreen mode Exit fullscreen mode

EDIT: first solution does not work for all possible test cases, this should

def solve(chips):
    chips.sort(reverse=True)
    return min(sum(chips) >> 1, sum(chips[1:]))
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lopert profile image
lopert

Doesn't this fail the [12,12,12] input?

Collapse
 
masterroshan profile image
TJ Johnson

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.

Collapse
 
masterroshan profile image
TJ Johnson

ah I see the correct output should be 18

Thread Thread
 
lopert profile image
lopert

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).

Thread Thread
 
masterroshan profile image
TJ Johnson • Edited

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)/2
which can be reduced mathematically to
(high + mid + low)/2
There 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