Find Number of Ways to Reach the K-th Stair - Combinatorics [JS]
Description Solution: Combinatorics We can use at most 1 more decrement operation than jumps. Go through each amount of jumps until we reach a stair where we don't have enough decrement operations to go back to k . For each amount of jumps, we reach a stair nextStair , and nextStair - k decrement operations to go back to k . The decrement operations can be performed in any order as long as they are not consecutive. There are a total of jumps + 1 positions where a decrement operation can be performed. Use the n-choose-r formula to calculate the number of combinations of putting r decrement operations in jumps + 1 different positions. e.g. k = 6 , and we use 2 jumps 1 -> 2 -> 4 -> 8 decrement operations = 8 - 6 = 2 positions: 1 _ 2 _ 4 _ 8 _ ( 4 available positions for 2 decrement operations) 4 choose 2 = 6 Time Complexity: O(log2(k)) Space Complexi...