Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2400.html

2400. Number of Ways to Reach a Position After Exactly k Steps

  • Difficulty: Medium.
  • Related Topics: Math, Dynamic Programming, Combinatorics.
  • Similar Questions: Unique Paths, Climbing Stairs, Reach a Number, Reaching Points, Number of Ways to Stay in the Same Place After Some Steps.

Problem

You are given two positive integers startPos and endPos. Initially, you are standing at position startPos on an infinite number line. With one step, you can move either one position to the left, or one position to the right.

Given a positive integer k, return the number of **different ways to reach the position endPos starting from startPos, such that you perform exactly k steps. Since the answer may be very large, return it **modulo 109 + 7.

Two ways are considered different if the order of the steps made is not exactly the same.

Note that the number line includes negative integers.

  Example 1:

Input: startPos = 1, endPos = 2, k = 3
Output: 3
Explanation: We can reach position 2 from 1 in exactly 3 steps in three ways:
- 1 -> 2 -> 3 -> 2.
- 1 -> 2 -> 1 -> 2.
- 1 -> 0 -> 1 -> 2.
It can be proven that no other way is possible, so we return 3.

Example 2:

Input: startPos = 2, endPos = 5, k = 10
Output: 0
Explanation: It is impossible to reach position 5 from position 2 in exactly 10 steps.

  Constraints:

  • 1 <= startPos, endPos, k <= 1000

Solution (Java, C++, Python)

  • class Solution {
        int p = 1000000007;
        public int numberOfWays(int a, int b, int k) {
            if ((a - b - k) % 2 != 0) return 0;
            if (Math.abs(a - b) > k) return 0;
            long res = 1L;
            for (int i = 0; i < (b - a + k) / 2; ++i) {
                res = res * (k - i) % p;
                res = res * inv(i + 1) % p;
            }
            return (int)res;
        }
    
        private long inv(long a) {
            if (a == 1) return 1;
            return (p - p / a) * inv(p % a) % p;
        }
    }
    
    ############
    
    class Solution {
        private Integer[][] f;
        private final int mod = (int) 1e9 + 7;
    
        public int numberOfWays(int startPos, int endPos, int k) {
            f = new Integer[k + 1][k + 1];
            return dfs(Math.abs(startPos - endPos), k);
        }
    
        private int dfs(int i, int j) {
            if (i > j || j < 0) {
                return 0;
            }
            if (j == 0) {
                return i == 0 ? 1 : 0;
            }
            if (f[i][j] != null) {
                return f[i][j];
            }
            int ans = dfs(i + 1, j - 1) + dfs(Math.abs(i - 1), j - 1);
            ans %= mod;
            return f[i][j] = ans;
        }
    }
    
  • class Solution:
        def numberOfWays(self, startPos: int, endPos: int, k: int) -> int:
            @cache
            def dfs(d, k):
                if k < 0 or abs(d) > k:
                    return 0
                if k == 0:
                    return d == 0
                res = dfs(d - 1, k - 1) + dfs(d + 1, k - 1)
                return res % (10**9 + 7)
    
            return dfs(abs(startPos - endPos), k)
    
    ############
    
    # 2400. Number of Ways to Reach a Position After Exactly k Steps
    # https://leetcode.com/problems/number-of-ways-to-reach-a-position-after-exactly-k-steps/
    
    class Solution:
        def numberOfWays(self, startPos: int, endPos: int, k: int) -> int:
            M = 10 ** 9 + 7
            
            @cache
            def dfs(pos, steps):
                if steps == k:
                    return 1 if pos == endPos else 0
                
                remainingSteps = k - steps
                
                if abs(pos - endPos) > remainingSteps:
                    return 0
                    
                return (dfs(pos + 1, steps + 1) + dfs(pos - 1, steps + 1)) % M
        
            return dfs(startPos, 0)
    
    
  • class Solution {
    public:
        int numberOfWays(int startPos, int endPos, int k) {
            const int mod = 1e9 + 7;
            int f[k + 1][k + 1];
            memset(f, -1, sizeof(f));
            function<int(int, int)> dfs = [&](int i, int j) -> int {
                if (i > j || j < 0) {
                    return 0;
                }
                if (j == 0) {
                    return i == 0 ? 1 : 0;
                }
                if (f[i][j] != -1) {
                    return f[i][j];
                }
                f[i][j] = (dfs(i + 1, j - 1) + dfs(abs(i - 1), j - 1)) % mod;
                return f[i][j];
            };
            return dfs(abs(startPos - endPos), k);
        }
    };
    
  • func numberOfWays(startPos int, endPos int, k int) int {
    	const mod = 1e9 + 7
    	f := make([][]int, k+1)
    	for i := range f {
    		f[i] = make([]int, k+1)
    		for j := range f[i] {
    			f[i][j] = -1
    		}
    	}
    	var dfs func(i, j int) int
    	dfs = func(i, j int) int {
    		if i > j || j < 0 {
    			return 0
    		}
    		if j == 0 {
    			if i == 0 {
    				return 1
    			}
    			return 0
    		}
    		if f[i][j] != -1 {
    			return f[i][j]
    		}
    		f[i][j] = (dfs(i+1, j-1) + dfs(abs(i-1), j-1)) % mod
    		return f[i][j]
    	}
    	return dfs(abs(startPos-endPos), k)
    }
    
    func abs(x int) int {
    	if x < 0 {
    		return -x
    	}
    	return x
    }
    
  • function numberOfWays(startPos: number, endPos: number, k: number): number {
        const mod = 10 ** 9 + 7;
        const f = new Array(k + 1).fill(0).map(() => new Array(k + 1).fill(-1));
        const dfs = (i: number, j: number): number => {
            if (i > j || j < 0) {
                return 0;
            }
            if (j === 0) {
                return i === 0 ? 1 : 0;
            }
            if (f[i][j] !== -1) {
                return f[i][j];
            }
            f[i][j] = dfs(i + 1, j - 1) + dfs(Math.abs(i - 1), j - 1);
            f[i][j] %= mod;
            return f[i][j];
        };
        return dfs(Math.abs(startPos - endPos), k);
    }
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions