I've been able to reduce my solution to constant time. Is it possible to reduce this by any constant factor further? (Besides trivialities i.e. inserting constants, not lazy importing). Specifically, I'd like to speed up
fastMinPayout if there is a way to do it without using log.
Lovely Lucky LAMBs
Being a henchman isn't all drudgery. Occasionally, when Commander Lambda is feeling generous, she'll hand out Lucky LAMBs (Lambda's All-purpose Money Bucks). Henchmen can use Lucky LAMBs to buy things like a second pair of socks, a pillow for their bunks, or even a third daily meal! However, actually passing out LAMBs isn't easy. Each henchman squad has a strict seniority ranking which must be respected - or else the henchmen will revolt and you'll all get demoted back to minions again!
There are 4 key rules which you must follow in order to avoid a revolt:
- The most junior henchman (with the least seniority) gets exactly 1 LAMB. (There will always be at least 1 henchman on a team.)
- A henchman will revolt if the person who ranks immediately above them gets more than double the number of LAMBs they do.
- A henchman will revolt if the amount of LAMBs given to their next two subordinates combined is more than the number of LAMBs they get. (Note that the two most junior henchmen won't have two subordinates, so this rule doesn't apply to them. The 2nd most junior henchman would require at least as many LAMBs as the most junior henchman.)
- You can always find more henchmen to pay - the Commander has plenty of employees. If there are enough LAMBs left over such that another henchman could be added as the most senior while obeying the other rules, you must always add and pay that henchman.
Note that you may not be able to hand out all the LAMBs. A single LAMB cannot be subdivided. That is, all henchmen must get a positive integer number of LAMBs.
Write a function called answer(total_lambs), where total_lambs is the integer number of LAMBs in the handout you are trying to divide. It should return an integer which represents the difference between the minimum and maximum number of henchmen who can share the LAMBs (that is, being as generous as possible to those you pay and as stingy as possible, respectively) while still obeying all of the above rules to avoid a revolt.
For instance, if you had 10 LAMBs and were as generous as possible, you could only pay 3 henchmen (1, 2, and 4 LAMBs, in order of ascending seniority), whereas if you were as stingy as possible, you could pay 4 henchmen (1, 1, 2, and 3 LAMBs). Therefore, answer(10) should return 4-3 = 1
To keep things interesting, Commander Lambda varies the sizes of the Lucky LAMB payouts: you can expect total_lambs to always be between 10 and 1 billion (10 ^ 9).
''' Recursive form Rules: 1) A0 = 1 2) An+1 !> 2*An 3) An-1 + An-2 !> An 4) n -> inf Rewritten: 1) A0 = 1 2) An <= 2*An-1 3) An >= An-1 + An-2 4) n -> inf Therefore: A0 = 1, A-1 = 0, A-2 = 0 An-1 + An-2 <= An <= 2*An-1 ''' def maxPayout(LAMBs): ''' Given An-1 + An-2 <= An <= 2*An-1 An is maximized when An = 2*An-1 ''' # payouts and payouts exist as 'dummy' payouts payouts = [0, 0, 1] LAMBs -= payouts[-1] while (LAMBs >= 0): payouts.append(2*payouts[-1]) LAMBs -= payouts[-1] # -2 for first two 'dummy' payouts and -1 for extra payout return len(payouts) - 2 - 1 def minPayout(LAMBs): ''' Given An-1 + An-2 <= An <= 2*An-1 An is minimized when An = An-1 + An-2 ''' # payouts and payouts exist as 'dummy' payouts payouts = [0, 0, 1] LAMBs -= payouts[-1] while (LAMBs >= 0): payouts.append(payouts[-1] + payouts[-2]) LAMBs -= payouts[-1] # -2 for first two 'dummy' payouts and -1 for extra payout return len(payouts) - 2 - 1 def solution(total_lambs): return minPayout(total_lambs) - maxPayout(total_lambs) def fastMaxPayout(LAMBs): ''' Since maxPayout follows An = 2*An-1, maxPayout follows geometric sequence that can be reduced to exponential. An = 2^n Then we solve for sum(An = 2^n) <= LAMBs and the geometric series' sum formula follows: Sn = A0 * 1-r^n / (1-r) Sn = 1 * 1-2^n / (1-2) Sn = 2^n - 1 Now we finally solve (Sn = 2^n - 1) <= LAMBs 2^n <= LAMBs + 1 n <= log2(LAMBs + 1) n = floor(log2(LAMBs + 1)) = (LAMBs + 1).bit_length() - 1 ''' return (LAMBs + 1).bit_length() - 1 def fastMinPayout(LAMBs): ''' Since minPayout follows An = An-1 + An-2, minPayout follows a shifted Fibonnacci sequence. Apply Binet's formula to derive the nth Fibonnacci sequence. An-1 = Fn = ((1+sqrt(5) / 2)^n - (1-sqrt(5) / 2)^n) / sqrt(5) Substitute constants for variables to simplify let a = 1+sqrt(5) / 2 let b = 1-sqrt(5) / 2 let x = An-1 * sqrt(5) x = a^n - b^n a^n = x + b^n n = loga(x + b^n)ls And since lim n->inf b^n = 0, Binet's formula approximates: n = loga(x) Now we finally solve (Sn = An+2 - 1) >= LAMBs n+3 = loga(An+2 * sqrt(5)) = loga((An+2 - 1 + 1) * sqrt(5)) n = loga((LAMBs + 1) * sqrt(5)) - 3 ''' from math import log, ceil, sqrt return int(ceil(log((LAMBs + 1 + 0.5)*sqrt(5), (1+sqrt(5)) / 2)) - 3) def fastSolution(total_lambs): return fastMinPayout(total_lambs) - fastMaxPayout(total_lambs) print(solution(143), fastSolution(143))