Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost Path with Alternating Directions II

Try to solve the Minimum Cost Path with Alternating Directions II problem.

Statement

You are given two integers m and n, representing the dimensions of an m $ imes$ n grid, and a 22D integer array waitCost of size m $ imes$ n.

You start at cell (0, 0) at second 11 by paying an initial entry cost of (0+1)imes(0+1)=1(0 + 1) imes (0 + 1) = 1. Your objective is to reach the bottom right cell (m - 1, n - 1).

At each second t≥1t \geq 1:

  • If tt is odd:

    • You may move to an adjacent cell, either right to (r, c + 1) or down to (r + 1, c). The direction of each move must alternate from the previous move, though the very first move can be in either direction. Entering cell (r, c) incurs a cost of (r+1)imes(c+1)(r + 1) imes (c + 1).

    • Alternatively, you may choose to wait at your current cell (r, c) and pay a cost of waitCost[r][c].

  • If tt is even:

    • You cannot move and must wait at your current cell (r, c), paying a cost of waitCost[r][c].

Return the minimum total cost required to reach cell (m - 1, n - 1).

Constraints:

  • 1≤1 \leq m, n ≤105\leq 10^5

  • 2≤2 \leq m $ imes$ n ≤105\leq 10^5

  • waitCost.length == m

  • waitCost[0].length == n

  • 0≤0 \leq waitCost[i][j] ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost Path with Alternating Directions II

Try to solve the Minimum Cost Path with Alternating Directions II problem.

Statement

You are given two integers m and n, representing the dimensions of an m $ imes$ n grid, and a 22D integer array waitCost of size m $ imes$ n.

You start at cell (0, 0) at second 11 by paying an initial entry cost of (0+1)imes(0+1)=1(0 + 1) imes (0 + 1) = 1. Your objective is to reach the bottom right cell (m - 1, n - 1).

At each second t≥1t \geq 1:

  • If tt is odd:

    • You may move to an adjacent cell, either right to (r, c + 1) or down to (r + 1, c). The direction of each move must alternate from the previous move, though the very first move can be in either direction. Entering cell (r, c) incurs a cost of (r+1)imes(c+1)(r + 1) imes (c + 1).

    • Alternatively, you may choose to wait at your current cell (r, c) and pay a cost of waitCost[r][c].

  • If tt is even:

    • You cannot move and must wait at your current cell (r, c), paying a cost of waitCost[r][c].

Return the minimum total cost required to reach cell (m - 1, n - 1).

Constraints:

  • 1≤1 \leq m, n ≤105\leq 10^5

  • 2≤2 \leq m $ imes$ n ≤105\leq 10^5

  • waitCost.length == m

  • waitCost[0].length == n

  • 0≤0 \leq waitCost[i][j] ≤105\leq 10^5