Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost Homecoming of a Robot in a Grid

Try to solve the Minimum Cost Homecoming of a Robot in a Grid problem.

Statement

There is an m×nm \times n grid where a robot starts at the coordinates startPos =[startrow,startcol]= [\text{start}_{\text{row}}, \text{start}_{\text{col}}] and aims to travel to its destination at homePos =[homerow,homecol]= [\text{home}_{\text{row}}, \text{home}_{\text{col}}].

At each step, the robot can move one cell up, down, left, or right. The cost of each movement depends only on the target cell:

  • Moving into any cell in row rr costs rowCosts[r].

  • Moving into any cell in column cc costs colCosts[c].

The robot does not pay any cost for its starting cell.

Return the minimum total cost for the robot to move from startPos to homePos.

Constraints:

  • m ==== rowCosts.length

  • n ==== colCosts.length

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

  • 0≤0 \leq rowCosts[r], colCosts[c] ≤104\leq 10^4

  • startPos.length ==2== 2

  • homePos.length ==2== 2

  • 0≤startrow,homerow<m0 \leq \text{start}_{\text{row}}, \text{home}_{\text{row}} < m

  • 0≤startcol,homecol<n0 \leq \text{start}_{\text{col}}, \text{home}_{\text{col}} < n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost Homecoming of a Robot in a Grid

Try to solve the Minimum Cost Homecoming of a Robot in a Grid problem.

Statement

There is an m×nm \times n grid where a robot starts at the coordinates startPos =[startrow,startcol]= [\text{start}_{\text{row}}, \text{start}_{\text{col}}] and aims to travel to its destination at homePos =[homerow,homecol]= [\text{home}_{\text{row}}, \text{home}_{\text{col}}].

At each step, the robot can move one cell up, down, left, or right. The cost of each movement depends only on the target cell:

  • Moving into any cell in row rr costs rowCosts[r].

  • Moving into any cell in column cc costs colCosts[c].

The robot does not pay any cost for its starting cell.

Return the minimum total cost for the robot to move from startPos to homePos.

Constraints:

  • m ==== rowCosts.length

  • n ==== colCosts.length

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

  • 0≤0 \leq rowCosts[r], colCosts[c] ≤104\leq 10^4

  • startPos.length ==2== 2

  • homePos.length ==2== 2

  • 0≤startrow,homerow<m0 \leq \text{start}_{\text{row}}, \text{home}_{\text{row}} < m

  • 0≤startcol,homecol<n0 \leq \text{start}_{\text{col}}, \text{home}_{\text{col}} < n