Int 3 for Programmers

When the other plan wins

When the other plan wins

Two hire companies quote in the same shape: a fixed charge plus a charge per day. Company one costs base1 + rate1 * days and company two costs base2 + rate2 * days, everything in pounds.

Write cheaper_after(base1, rate1, base2, rate2), which returns the smallest whole number of days at which company two costs strictly less than company one. If that never happens for any number of days from 0 to 10000, return -1.

Worked example, with base1 = 30, rate1 = 2, base2 = 0 and rate2 = 6. At 0 days company two costs 0 against 30, so it is already cheaper and the answer is 0.

Second worked example, with base1 = 10, rate1 = 9, base2 = 50 and rate2 = 4. At 8 days the costs are 82 and 82, which is a tie rather than a win. At 9 days they are 91 and 86, so the answer is 9.

  • Constraints: every value is between 0 and 1000.
  • Strictly less means a tie does not count.
  • If the two lines never meet, because company two starts dearer and rises at least as fast, no day works and the answer is -1.
  • Searching day by day up to 10000 is fast enough here, and reasoning it out with algebra is also fine.
Code

Run your code to see how it does on the examples. Submit also runs the hidden cases.