Hacker News new | ask | show | jobs
by yoshicoder 585 days ago
I got nerd sniped by this question, and I think I have a closed form formula. Obviously it needs a summation since the order/placement of the numbers matters.

The formula is 2^(# of questions - # terminating questions) + SUM[2^(terminiating question number - idx in terminating question list] where the SUM is over all the terms in the list given as input to the question_paths function

for example: we take the example of question_paths(10,[1,5]), where the answer is 274. From the formula, we take 2^(10-2) + 2^(1-0) + 2^(5-1) which equals 274.

similarly for question_paths(10,[6,8]), where the answer is 448. From the formula, we take 2^(10-2) + 2^(6-0) + 2^(8-1) which equals 448.

1 comments

I wrote some python code which should give the right answer

  def formula(total_qs, terminating):
    total = 1 << (total_qs - len(terminating))
    for i, num in enumerate(terminating):
        total += 1 << (num - i)
    return total

  print(formula(10, [1, 5]))```