You are given two integers m and n representing the number of rows and columns of a grid, respectively.
The cost to enter cell (i, j) is defined as (i + 1) * (j + 1).
You are also given a 2D integer array waitCost where waitCost[i][j] defines the cost to wait on that cell.
The path will always begin by entering cell (0, 0) on move 1 and paying the entrance cost.
At each step, you follow an alternating pattern:
waitCost[i][j] during that second.Return the minimum total cost required to reach (m - 1, n - 1).
Example 1:
Input: m = 1, n = 2, waitCost = [[1,2]]
Output: 3
Explanation:
The optimal path is:
(0, 0) at second 1 with entry cost (0 + 1) * (0 + 1) = 1.(0, 1) with entry cost (0 + 1) * (1 + 1) = 2.Thus, the total cost is 1 + 2 = 3.
Example 2:
Input: m = 2, n = 2, waitCost = [[3,5],[2,4]]
Output: 9
Explanation:
The optimal path is:
(0, 0) at second 1 with entry cost (0 + 1) * (0 + 1) = 1.(1, 0) with entry cost (1 + 1) * (0 + 1) = 2.(1, 0), paying waitCost[1][0] = 2.(1, 1) with entry cost (1 + 1) * (1 + 1) = 4.Thus, the total cost is 1 + 2 + 2 + 4 = 9.
Example 3:
Input: m = 2, n = 3, waitCost = [[6,1,4],[3,2,5]]
Output: 16
Explanation:
The optimal path is:
(0, 0) at second 1 with entry cost (0 + 1) * (0 + 1) = 1.(0, 1) with entry cost (0 + 1) * (1 + 1) = 2.(0, 1), paying waitCost[0][1] = 1.(1, 1) with entry cost (1 + 1) * (1 + 1) = 4.(1, 1), paying waitCost[1][1] = 2.(1, 2) with entry cost (1 + 1) * (2 + 1) = 6.Thus, the total cost is 1 + 2 + 1 + 4 + 2 + 6 = 16.
Constraints:
1 <= m, n <= 1052 <= m * n <= 105waitCost.length == mwaitCost[0].length == n0 <= waitCost[i][j] <= 105Problem Overview: You are given a cost matrix and need the minimum cost to travel from the top-left cell to the bottom-right cell. The constraint: consecutive moves must alternate direction types (horizontal vs vertical). Every move adds the cost of the destination cell, so the challenge is tracking both position and the last direction used.
Approach 1: Brute Force DFS with Direction Tracking (Exponential Time, O(2^(m*n)) time, O(m*n) recursion space)
Start a depth‑first search from (0,0). At each cell, try valid moves that alternate direction from the previous step. For example, if the last move was horizontal, the next must be vertical. Track visited states and accumulate path cost until reaching the bottom-right cell. This approach explores many repeated states because the same cell can be reached with different previous directions, making the runtime exponential. Useful mainly for understanding the constraint and validating small inputs.
Approach 2: Dynamic Programming with Direction State (O(m*n) time, O(m*n) space)
The key observation: the optimal cost for a cell depends on both its coordinates and the direction used to reach it. Maintain two DP states per cell: dp[i][j][0] for arriving via a horizontal move and dp[i][j][1] for arriving via a vertical move. When transitioning, enforce alternation: horizontal states transition from vertical states and vice versa. Iterate through the grid and update costs using the minimum previous valid state. Each state is processed once, giving O(m*n) time with constant work per transition.
Using a direction-aware DP avoids recomputation and models the constraint directly in the state. The idea is similar to grid problems where movement history matters, commonly solved with Dynamic Programming on a Matrix. The grid itself is treated like a graph where each cell has two directional states, a pattern also seen in constrained path problems over Array-based grids.
Recommended for interviews: The dynamic programming approach with a direction state is what interviewers expect. A brute force DFS demonstrates understanding of the alternating constraint, but the DP solution shows you can convert repeated subproblems into a state transition and reduce the complexity to linear in the number of cells.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force DFS with Direction Tracking | O(2^(m*n)) | O(m*n) | Conceptual baseline or very small grids |
| Dynamic Programming with Direction State | O(m*n) | O(m*n) | General case and expected interview solution |
Leetcode Biweekly Contest 160 - Q2. Minimum Cost Path with Alternating Directions II • ADevOpsBeginner • 405 views views
Watch 6 more video solutions →Practice Minimum Cost Path with Alternating Directions II with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor