Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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: On odd-numbered seconds, you must move right or down to an adjacent cell, paying its entry cost. On even-numbered seconds, you must wait in place for exactly one second and pay waitCost[i][j] during that second. Return the minimum total cost required to reach (m - 1, n - 1).
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
1 2 [[1,2]]
2 2 [[3,5],[2,4]]
2 3 [[6,1,4],[3,2,5]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3603: Minimum Cost Path with Alternating Directions II
// Auto-generated Java example from go.
class Solution {
public void exampleSolution() {
}
}
// Reference (go):
// // Accepted solution for LeetCode #3603: Minimum Cost Path with Alternating Directions II
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minCost1(m, n int, waitCost [][]int) int64 {
// memo := make([][]int, m)
// for i := range memo {
// memo[i] = make([]int, n)
// }
// var dfs func(int, int) int
// dfs = func(i, j int) (res int) {
// if i < 0 || j < 0 {
// return math.MaxInt
// }
// if i == 0 && j == 0 {
// return 1 // 起点只有 1 的进入成本,不需要等待
// }
// p := &memo[i][j]
// if *p == 0 {
// *p = min(dfs(i, j-1), dfs(i-1, j)) + waitCost[i][j] + (i+1)*(j+1)
// }
// return *p
// }
// return int64(dfs(m-1, n-1) - waitCost[m-1][n-1]) // 终点不需要等待
// }
//
// func minCost2(m, n int, waitCost [][]int) int64 {
// f := make([][]int, m+1)
// for i := range f {
// f[i] = make([]int, n+1)
// }
// f[0][1] = -waitCost[0][0] // 计算 f[1][1] 的时候抵消掉
// for j := 2; j <= n; j++ {
// f[0][j] = math.MaxInt
// }
// for i, row := range waitCost {
// f[i+1][0] = math.MaxInt
// for j, c := range row {
// f[i+1][j+1] = min(f[i+1][j], f[i][j+1]) + c + (i+1)*(j+1)
// }
// }
// return int64(f[m][n] - waitCost[m-1][n-1])
// }
//
// func minCost3(m, n int, waitCost [][]int) int64 {
// f := make([]int, n+1)
// for j := range f {
// f[j] = math.MaxInt
// }
// f[1] = -waitCost[0][0]
// for i, row := range waitCost {
// for j, c := range row {
// f[j+1] = min(f[j], f[j+1]) + c + (i+1)*(j+1)
// }
// }
// return int64(f[n] - waitCost[m-1][n-1])
// }
//
// func minCost(m, n int, f [][]int) int64 {
// f[0][0] = 1
// f[m-1][n-1] = 0
// for j := 1; j < n; j++ {
// f[0][j] += f[0][j-1] + j + 1
// }
// for i := 1; i < m; i++ {
// f[i][0] += f[i-1][0] + i + 1
// for j := 1; j < n; j++ {
// f[i][j] += min(f[i][j-1], f[i-1][j]) + (i+1)*(j+1)
// }
// }
// return int64(f[m-1][n-1])
// }
// Accepted solution for LeetCode #3603: Minimum Cost Path with Alternating Directions II
package main
import "math"
// https://space.bilibili.com/206214
func minCost1(m, n int, waitCost [][]int) int64 {
memo := make([][]int, m)
for i := range memo {
memo[i] = make([]int, n)
}
var dfs func(int, int) int
dfs = func(i, j int) (res int) {
if i < 0 || j < 0 {
return math.MaxInt
}
if i == 0 && j == 0 {
return 1 // 起点只有 1 的进入成本,不需要等待
}
p := &memo[i][j]
if *p == 0 {
*p = min(dfs(i, j-1), dfs(i-1, j)) + waitCost[i][j] + (i+1)*(j+1)
}
return *p
}
return int64(dfs(m-1, n-1) - waitCost[m-1][n-1]) // 终点不需要等待
}
func minCost2(m, n int, waitCost [][]int) int64 {
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
f[0][1] = -waitCost[0][0] // 计算 f[1][1] 的时候抵消掉
for j := 2; j <= n; j++ {
f[0][j] = math.MaxInt
}
for i, row := range waitCost {
f[i+1][0] = math.MaxInt
for j, c := range row {
f[i+1][j+1] = min(f[i+1][j], f[i][j+1]) + c + (i+1)*(j+1)
}
}
return int64(f[m][n] - waitCost[m-1][n-1])
}
func minCost3(m, n int, waitCost [][]int) int64 {
f := make([]int, n+1)
for j := range f {
f[j] = math.MaxInt
}
f[1] = -waitCost[0][0]
for i, row := range waitCost {
for j, c := range row {
f[j+1] = min(f[j], f[j+1]) + c + (i+1)*(j+1)
}
}
return int64(f[n] - waitCost[m-1][n-1])
}
func minCost(m, n int, f [][]int) int64 {
f[0][0] = 1
f[m-1][n-1] = 0
for j := 1; j < n; j++ {
f[0][j] += f[0][j-1] + j + 1
}
for i := 1; i < m; i++ {
f[i][0] += f[i-1][0] + i + 1
for j := 1; j < n; j++ {
f[i][j] += min(f[i][j-1], f[i-1][j]) + (i+1)*(j+1)
}
}
return int64(f[m-1][n-1])
}
# Accepted solution for LeetCode #3603: Minimum Cost Path with Alternating Directions II
# Time: O(m * n)
# Space: O(1)
# dp
class Solution(object):
def minCost(self, m, n, waitCost):
"""
:type m: int
:type n: int
:type waitCost: List[List[int]]
:rtype: int
"""
waitCost[0][0] = waitCost[m-1][n-1] = 0
for i in xrange(m):
for j in xrange(n):
prev = 0 if (i, j) == (0, 0) else float("inf")
if i-1 >= 0:
prev = min(prev, waitCost[i-1][j])
if j-1 >= 0:
prev = min(prev, waitCost[i][j-1])
waitCost[i][j] += prev+(i+1)*(j+1)
return waitCost[m-1][n-1]
# Time: O(m * n)
# Space: O(n)
# dp
class Solution2(object):
def minCost(self, m, n, waitCost):
"""
:type m: int
:type n: int
:type waitCost: List[List[int]]
:rtype: int
"""
waitCost[0][0] = waitCost[m-1][n-1] = 0
dp = [0]*n
for i in xrange(m):
for j in xrange(n):
prev = 0 if (i, j) == (0, 0) else float("inf")
if i-1 >= 0:
prev = min(prev, dp[j])
if j-1 >= 0:
prev = min(prev, dp[j-1])
dp[j] = prev+waitCost[i][j]+(i+1)*(j+1)
return dp[n-1]
// Accepted solution for LeetCode #3603: Minimum Cost Path with Alternating Directions II
// Rust example auto-generated from go reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (go):
// // Accepted solution for LeetCode #3603: Minimum Cost Path with Alternating Directions II
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minCost1(m, n int, waitCost [][]int) int64 {
// memo := make([][]int, m)
// for i := range memo {
// memo[i] = make([]int, n)
// }
// var dfs func(int, int) int
// dfs = func(i, j int) (res int) {
// if i < 0 || j < 0 {
// return math.MaxInt
// }
// if i == 0 && j == 0 {
// return 1 // 起点只有 1 的进入成本,不需要等待
// }
// p := &memo[i][j]
// if *p == 0 {
// *p = min(dfs(i, j-1), dfs(i-1, j)) + waitCost[i][j] + (i+1)*(j+1)
// }
// return *p
// }
// return int64(dfs(m-1, n-1) - waitCost[m-1][n-1]) // 终点不需要等待
// }
//
// func minCost2(m, n int, waitCost [][]int) int64 {
// f := make([][]int, m+1)
// for i := range f {
// f[i] = make([]int, n+1)
// }
// f[0][1] = -waitCost[0][0] // 计算 f[1][1] 的时候抵消掉
// for j := 2; j <= n; j++ {
// f[0][j] = math.MaxInt
// }
// for i, row := range waitCost {
// f[i+1][0] = math.MaxInt
// for j, c := range row {
// f[i+1][j+1] = min(f[i+1][j], f[i][j+1]) + c + (i+1)*(j+1)
// }
// }
// return int64(f[m][n] - waitCost[m-1][n-1])
// }
//
// func minCost3(m, n int, waitCost [][]int) int64 {
// f := make([]int, n+1)
// for j := range f {
// f[j] = math.MaxInt
// }
// f[1] = -waitCost[0][0]
// for i, row := range waitCost {
// for j, c := range row {
// f[j+1] = min(f[j], f[j+1]) + c + (i+1)*(j+1)
// }
// }
// return int64(f[n] - waitCost[m-1][n-1])
// }
//
// func minCost(m, n int, f [][]int) int64 {
// f[0][0] = 1
// f[m-1][n-1] = 0
// for j := 1; j < n; j++ {
// f[0][j] += f[0][j-1] + j + 1
// }
// for i := 1; i < m; i++ {
// f[i][0] += f[i-1][0] + i + 1
// for j := 1; j < n; j++ {
// f[i][j] += min(f[i][j-1], f[i-1][j]) + (i+1)*(j+1)
// }
// }
// return int64(f[m-1][n-1])
// }
// Accepted solution for LeetCode #3603: Minimum Cost Path with Alternating Directions II
// Auto-generated TypeScript example from go.
function exampleSolution(): void {
}
// Reference (go):
// // Accepted solution for LeetCode #3603: Minimum Cost Path with Alternating Directions II
// package main
//
// import "math"
//
// // https://space.bilibili.com/206214
// func minCost1(m, n int, waitCost [][]int) int64 {
// memo := make([][]int, m)
// for i := range memo {
// memo[i] = make([]int, n)
// }
// var dfs func(int, int) int
// dfs = func(i, j int) (res int) {
// if i < 0 || j < 0 {
// return math.MaxInt
// }
// if i == 0 && j == 0 {
// return 1 // 起点只有 1 的进入成本,不需要等待
// }
// p := &memo[i][j]
// if *p == 0 {
// *p = min(dfs(i, j-1), dfs(i-1, j)) + waitCost[i][j] + (i+1)*(j+1)
// }
// return *p
// }
// return int64(dfs(m-1, n-1) - waitCost[m-1][n-1]) // 终点不需要等待
// }
//
// func minCost2(m, n int, waitCost [][]int) int64 {
// f := make([][]int, m+1)
// for i := range f {
// f[i] = make([]int, n+1)
// }
// f[0][1] = -waitCost[0][0] // 计算 f[1][1] 的时候抵消掉
// for j := 2; j <= n; j++ {
// f[0][j] = math.MaxInt
// }
// for i, row := range waitCost {
// f[i+1][0] = math.MaxInt
// for j, c := range row {
// f[i+1][j+1] = min(f[i+1][j], f[i][j+1]) + c + (i+1)*(j+1)
// }
// }
// return int64(f[m][n] - waitCost[m-1][n-1])
// }
//
// func minCost3(m, n int, waitCost [][]int) int64 {
// f := make([]int, n+1)
// for j := range f {
// f[j] = math.MaxInt
// }
// f[1] = -waitCost[0][0]
// for i, row := range waitCost {
// for j, c := range row {
// f[j+1] = min(f[j], f[j+1]) + c + (i+1)*(j+1)
// }
// }
// return int64(f[n] - waitCost[m-1][n-1])
// }
//
// func minCost(m, n int, f [][]int) int64 {
// f[0][0] = 1
// f[m-1][n-1] = 0
// for j := 1; j < n; j++ {
// f[0][j] += f[0][j-1] + j + 1
// }
// for i := 1; i < m; i++ {
// f[i][0] += f[i-1][0] + i + 1
// for j := 1; j < n; j++ {
// f[i][j] += min(f[i][j-1], f[i-1][j]) + (i+1)*(j+1)
// }
// }
// return int64(f[m-1][n-1])
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.