Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost Homecoming of a Robot in a Grid

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

Statement

There is an mimesnm imes n grid where a robot starts at the coordinates startPos =[extstartextrow,extstartextcol]= [ ext{start}_{ ext{row}}, ext{start}_{ ext{col}}] and aims to travel to its destination at homePos =[exthomeextrow,exthomeextcol]= [ ext{home}_{ ext{row}}, ext{home}_{ ext{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≤extstartextrow,exthomeextrow<m0 \leq ext{start}_{ ext{row}}, ext{home}_{ ext{row}} < m

  • 0≤extstartextcol,exthomeextcol<n0 \leq ext{start}_{ ext{col}}, ext{home}_{ ext{col}} < n

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Cost Homecoming of a Robot in a Grid

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

Statement

There is an mimesnm imes n grid where a robot starts at the coordinates startPos =[extstartextrow,extstartextcol]= [ ext{start}_{ ext{row}}, ext{start}_{ ext{col}}] and aims to travel to its destination at homePos =[exthomeextrow,exthomeextcol]= [ ext{home}_{ ext{row}}, ext{home}_{ ext{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≤extstartextrow,exthomeextrow<m0 \leq ext{start}_{ ext{row}}, ext{home}_{ ext{row}} < m

  • 0≤extstartextcol,exthomeextcol<n0 \leq ext{start}_{ ext{col}}, ext{home}_{ ext{col}} < n