#2087
Minimum Cost Homecoming of a Robot in a Grid
specialist · 855 · lc medium +31 · verified · 51.6% accepted · 745 likes · top 41%
Description
A robot on an m x n grid starts at startPos = [startrow, startcol] and needs to reach homePos = [homerow, homecol]. Moving into row r costs rowCosts[r]; moving into column c costs colCosts[c]. All shortest paths from start to home share the same total cost. Return that minimum cost.
Example 1:
Input: startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7]
Output: 18
Explanation: One optimal path is that:
Starting from (1, 0)
-> It goes down to (2, 0). This move costs rowCosts[2] = 3.
-> It goes right to (2, 1). This move costs colCosts[1] = 2.
-> It goes right to (2, 2). This move costs colCosts[2] = 6.
-> It goes right to (2, 3). This move costs colCosts[3] = 7.
The total cost is 3 + 2 + 6 + 7 = 18
Example 2:
Input: startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26]
Output: 0
Explanation: The robot is already at its home. Since no moves occur, the total cost is 0.
Code
1
2
3