# Program to count number of ways to win at most k consecutive games in Python

PythonServer Side ProgrammingProgramming

Suppose we have two numbers n and k. Here n represents the number of games we are going to play. We have to find in how many ways we can win k or fewer games consecutively. If the answer is too large then mod the result by 10^9 + 7.

So, if the input is like n = 3 k = 2, then the output will be 7, as the possible ways in which we can win 2 or fewer times consecutively, are ["LLL", "WLL", "LWL", "LLW", "WWL", "LWW", "WLW"]

To solve this, we will follow these steps −

• m := 1^9 + 7
• Define a function dp() . This will take i, K
• if i >= n or K > k, then
• return true when K <= k, otherwise false
• return dp(i + 1, 0) mod m + dp(i + 1, K + 1) mod m
• From the main method, do the following −
• return dp(0, 0) mod m

## Example

Let us see the following implementation to get better understanding −

def solve(n, k):
m = 1**9 + 7

def dp(i, K):
if i >= n or K > k:
return K <= k
return dp(i + 1, 0) % m + dp(i + 1, K + 1) % m

return dp(0, 0) % m

n = 4
k = 2
print(solve(n, k))

## Input

4, 2

## Output

5
Updated on 18-Oct-2021 10:55:26