Monday, 27 April 2020

Halloween Sale

Algorithm:
1. Find n = floor((p - m)/d)
2. check if s - np + (n)(n - 1)(d/2) > 0
    this formula comes from observation of how much money we have with us after each purchase
    s, s - p, s - p + d, s - 2p + 3d, ...... , s - np + (n)(n - 1)(d/2)
    if this is > 0, that means even after the minimum price is reached, more items can be bought.
    now edge cases here are for the base cases (n = 1 and n = 0)
    the n = 0 edge case is when s < p (no items can be bought)
    the n = 1 edge case is when s - p < p - d (one item can be bought)
    these edge cases have to be separately dealt with, using if statements prior to step 2
3. if the answer to step 2 is yes, then
    s = s - np + (n)(n - 1)(d/2)
    n += floor(s/m)
 4. if the answer to step 2 is no, then we solve the quadratic equation:
     s - np + (n)(n - 1)(d/2) = 0
     to find the number of items we can buy, and then floor the result.
     I don't know why, but taking the smaller root worked, but not always the bigger root.
5. return n

No comments:

Post a Comment