java.lang.Object
g2001_2100.s2087_minimum_cost_homecoming_of_a_robot_in_a_grid.Solution

public class Solution extends Object
2087 - Minimum Cost Homecoming of a Robot in a Grid.<p>Medium</p> <p>There is an <code>m x n</code> grid, where <code>(0, 0)</code> is the top-left cell and <code>(m - 1, n - 1)</code> is the bottom-right cell. You are given an integer array <code>startPos</code> where <code>startPos = [start<sub>row</sub>, start<sub>col</sub>]</code> indicates that <strong>initially</strong> , a <strong>robot</strong> is at the cell <code>(start<sub>row</sub>, start<sub>col</sub>)</code>. You are also given an integer array <code>homePos</code> where <code>homePos = [home<sub>row</sub>, home<sub>col</sub>]</code> indicates that its <strong>home</strong> is at the cell <code>(home<sub>row</sub>, home<sub>col</sub>)</code>.</p> <p>The robot needs to go to its home. It can move one cell in four directions: <strong>left</strong> , <strong>right</strong> , <strong>up</strong> , or <strong>down</strong> , and it can not move outside the boundary. Every move incurs some cost. You are further given two <strong>0-indexed</strong> integer arrays: <code>rowCosts</code> of length <code>m</code> and <code>colCosts</code> of length <code>n</code>.</p> <ul> <li>If the robot moves <strong>up</strong> or <strong>down</strong> into a cell whose <strong>row</strong> is <code>r</code>, then this move costs <code>rowCosts[r]</code>.</li> <li>If the robot moves <strong>left</strong> or <strong>right</strong> into a cell whose <strong>column</strong> is <code>c</code>, then this move costs <code>colCosts[c]</code>.</li> </ul> <p>Return <em>the <strong>minimum total cost</strong> for this robot to return home</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/10/11/eg-1.png" alt="" /></p> <p><strong>Input:</strong> startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7]</p> <p><strong>Output:</strong> 18</p> <p><strong>Explanation:</strong> One optimal path is that:</p> <p>Starting from (1, 0)</p> <p>-> It goes down to ( <strong>2</strong> , 0). This move costs rowCosts[2] = 3.</p> <p>-> It goes right to (2, <strong>1</strong> ). This move costs colCosts[1] = 2.</p> <p>-> It goes right to (2, <strong>2</strong> ). This move costs colCosts[2] = 6.</p> <p>-> It goes right to (2, <strong>3</strong> ). This move costs colCosts[3] = 7.</p> <p>The total cost is 3 + 2 + 6 + 7 = 18</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26]</p> <p><strong>Output:</strong> 0</p> <p><strong>Explanation:</strong> The robot is already at its home. Since no moves occur, the total cost is 0.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>m == rowCosts.length</code></li> <li><code>n == colCosts.length</code></li> <li><code>1 <= m, n <= 10<sup>5</sup></code></li> <li><code>0 <= rowCosts[r], colCosts[c] <= 10<sup>4</sup></code></li> <li><code>startPos.length == 2</code></li> <li><code>homePos.length == 2</code></li> <li><code>0 <= start<sub>row</sub>, home<sub>row</sub> < m</code></li> <li><code>0 <= start<sub>col</sub>, home<sub>col</sub> < n</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minCost

      public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts)