I was asked to play the Google foobar challenge. I am now at level 2.2 with the following question.
Lovely Lucky LAMBs
Being a hangover is not all hard. Sometimes when Commander Lambda feels generous, she will give out Lucky LAMBs (Lambda All-Cost Money Bucks). A henchman can use the Lucky LAMB to buy items such as a second pair of socks, a pillow for beds, or even a third daily meal! However, actually transmitting LAMBs is not easy. Each hanchman team has a strict seniority rating that needs to be respected, or henchmen will rebel and you will again be demoted to minions!
There are four key rules that you must follow in order to avoid rebellion:
- The youngest henchman (with the least seniority) receives exactly 1 LAMB. (There will always be at least 1 henchman in a team.)
- The henchman will rebel if the person who is directly above them receives more than twice the amount of LAMB they make.
- The hangover will revolt if the number of LAMBs transferred to their next two subordinates is greater than the number of LAMBs they receive. (Please note that the two younger minions will not have two subordinates, therefore this rule does not apply to them. The 2nd younger minion will need at least as many LAMBs as the younger henchman.)
- , - . LAMB, , , , .
, LAMB. LAMB . , LAMB.
, answer (total_lambs), total_lambs - LAMB , . , , LAMB ( , ), , .
, 10 LAMB , 3 (1, 2 4 LAMB, ), , , 4 (1, 1, 2 3 LAMB). (10) 4-3 = 1. , Commander Lambda Lucky LAMB: , total_lambs 10 1 (10 ^ 9).
, LAMB , 1,2,4,8...
($ S = 2 ^ n -1 $, [log_2 (S + 1)]
, LAMB , , -, fibbonaci 1, 1, 2, 3, 5... :
python:
from math import sqrt
from math import log
from math import pow
def answer(total_lambs):
phi = (1+sqrt(5))/2
tau = (1-sqrt(5))/2
eps = pow(10, -10)
max_hunchmen = int(round(log((total_lambs + 1) * sqrt(5)+eps, phi))) - 2
Fib_num = int(round((pow(phi, max_hunchmen+2)-pow(tau,max_hunchmen+2))/sqrt(5)))
if total_lambs+1 < Fib_num:
max_hunchmen -= 1
min_hunchmen = int(log((total_lambs + 1), 2))
return abs(max_hunchmen - min_hunchmen)
10 (Google ). 8 . , , . / . !!