Number of Ways to Reach Destination in the Grid — LeetCode 2912 Python Solution

HardLeetCode PremiumMathDynamic ProgrammingCombinatorics
Problem
#2912
Reading time
3 min

The problem

You are given two integers n and m which represent the size of a 1-indexed grid. You are also given an integer k, a 1-indexed integer array source and a 1-indexed integer array dest, where source and dest are in the form [x, y] representing a cell on the given grid.

Example

Input
n = 3, m = 2, k = 2, source = [1,1], dest = [2,2]
Output
2
Explanation
There are 2 possible sequences of reaching [2,2] from [1,1]:

Python solution

Python
class Solution:
    def numberOfWays(
        self, n: int, m: int, k: int, source: List[int], dest: List[int]
    ) -> int:
        mod = 10**9 + 7
        a, b, c, d = 1, 0, 0, 0
        for _ in range(k):
            aa = ((n - 1) * b + (m - 1) * c) % mod
            bb = (a + (n - 2) * b + (m - 1) * d) % mod
            cc = (a + (m - 2) * c + (n - 1) * d) % mod
            dd = (b + c + (n - 2) * d + (m - 2) * d) % mod
            a, b, c, d = aa, bb, cc, dd
        if source[0] == dest[0]:
            return a if source[1] == dest[1] else c
        return b if source[1] == dest[1] else d

Complexity

MeasureComplexity
TimeO(k), where k is the number of moves
SpaceO(1) auxiliary

Pattern: Dynamic Programming

Define a state, write the transition, and stop recomputing the same subproblem. LeetCode 2912. Number of Ways to Reach Destination in the Grid is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Dynamic Programming.

The dynamic programming guide has the Python template for the pattern and the 481 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2912. Number of Ways to Reach Destination in the Grid?
LeetCode 2912. Number of Ways to Reach Destination in the Grid is rated Hard on LeetCode.
What is the time complexity of LeetCode 2912. Number of Ways to Reach Destination in the Grid?
The Python solution on this page runs in O(k), where k is the number of moves.
What is the space complexity of LeetCode 2912. Number of Ways to Reach Destination in the Grid?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2912. Number of Ways to Reach Destination in the Grid cover?
LeetCode 2912. Number of Ways to Reach Destination in the Grid is tagged Math, Dynamic Programming and Combinatorics on LeetCode.
Is LeetCode 2912. Number of Ways to Reach Destination in the Grid a premium problem?
Yes. LeetCode 2912. Number of Ways to Reach Destination in the Grid is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview