> I prefer Rhett Butler's solution.
¿what's that?
> can you please explain the possible() function a little more
1 2 3 4 5 6
|
def possible(demand, cost, balloons, target):
"""try to satisfy the demand without exceeding target cost or running out of balloons"""
for d, c in zip(demand, cost):
balloons -= how_many(d, c, target)
return balloons >= 0
|
let's just analyse one single day
for example, you want 6 balloons and each balloon may be changed by 3 candies
¿can you do it with a cost of at most 8? ¿how many balloons do you need to give for that? ¿do you have that many balloons?
then to solve for a list you simply repeat the question in each day
suppose that you cannot solve for 8, then you increase, say to 13, you can so you decrease to 10... and so on
there you do a binary search to get in time
there still remains what's the upper limit, but I guess you can figure out that, and if not just put something really big.