{"id":8816,"date":"2021-11-27T09:18:35","date_gmt":"2021-11-27T17:18:35","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8816"},"modified":"2021-11-27T09:20:07","modified_gmt":"2021-11-27T17:20:07","slug":"leetcode-2087-minimum-cost-homecoming-of-a-robot-in-a-grid","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2087-minimum-cost-homecoming-of-a-robot-in-a-grid\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2087. Minimum Cost Homecoming of a Robot in a Grid"},"content":{"rendered":"\n<p>There is an&nbsp;<code>m x n<\/code>&nbsp;grid, where&nbsp;<code>(0, 0)<\/code>&nbsp;is the top-left cell and&nbsp;<code>(m - 1, n - 1)<\/code>&nbsp;is the bottom-right cell. You are given an integer array&nbsp;<code>startPos<\/code>&nbsp;where&nbsp;<code>startPos = [start<sub>row<\/sub>, start<sub>col<\/sub>]<\/code>&nbsp;indicates that&nbsp;<strong>initially<\/strong>, a&nbsp;<strong>robot<\/strong>&nbsp;is at the cell&nbsp;<code>(start<sub>row<\/sub>, start<sub>col<\/sub>)<\/code>. You are also given an integer array&nbsp;<code>homePos<\/code>&nbsp;where&nbsp;<code>homePos = [home<sub>row<\/sub>, home<sub>col<\/sub>]<\/code>&nbsp;indicates that its&nbsp;<strong>home<\/strong>&nbsp;is at the cell&nbsp;<code>(home<sub>row<\/sub>, home<sub>col<\/sub>)<\/code>.<\/p>\n\n\n\n<p>The robot needs to go to its home. It can move one cell in four directions:&nbsp;<strong>left<\/strong>,&nbsp;<strong>right<\/strong>,&nbsp;<strong>up<\/strong>, or&nbsp;<strong>down<\/strong>, and it can not move outside the boundary. Every move incurs some cost. You are further given two&nbsp;<strong>0-indexed<\/strong>&nbsp;integer arrays:&nbsp;<code>rowCosts<\/code>&nbsp;of length&nbsp;<code>m<\/code>&nbsp;and&nbsp;<code>colCosts<\/code>&nbsp;of length&nbsp;<code>n<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If the robot moves&nbsp;<strong>up<\/strong>&nbsp;or&nbsp;<strong>down<\/strong>&nbsp;into a cell whose&nbsp;<strong>row<\/strong>&nbsp;is&nbsp;<code>r<\/code>, then this move costs&nbsp;<code>rowCosts[r]<\/code>.<\/li><li>If the robot moves&nbsp;<strong>left<\/strong>&nbsp;or&nbsp;<strong>right<\/strong>&nbsp;into a cell whose&nbsp;<strong>column<\/strong>&nbsp;is&nbsp;<code>c<\/code>, then this move costs&nbsp;<code>colCosts[c]<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum total cost<\/strong>&nbsp;for this robot to return home<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/11\/eg-1.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7]\n<strong>Output:<\/strong> 18\n<strong>Explanation:<\/strong> One optimal path is that:\nStarting from (1, 0)\n-&gt; It goes down to (<strong>2<\/strong>, 0). This move costs rowCosts[2] = 3.\n-&gt; It goes right to (2, <strong>1<\/strong>). This move costs colCosts[1] = 2.\n-&gt; It goes right to (2, <strong>2<\/strong>). This move costs colCosts[2] = 6.\n-&gt; It goes right to (2, <strong>3<\/strong>). This move costs colCosts[3] = 7.\nThe total cost is 3 + 2 + 6 + 7 = 18<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> The robot is already at its home. Since no moves occur, the total cost is 0.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>m == rowCosts.length<\/code><\/li><li><code>n == colCosts.length<\/code><\/li><li><code>1 &lt;= m, n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= rowCosts[r], colCosts[c] &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>startPos.length == 2<\/code><\/li><li><code>homePos.length == 2<\/code><\/li><li><code>0 &lt;= start<sub>row<\/sub>, home<sub>row<\/sub>&nbsp;&lt; m<\/code><\/li><li><code>0 &lt;= start<sub>col<\/sub>, home<sub>col<\/sub>&nbsp;&lt; n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Manhattan distance<\/strong><\/h2>\n\n\n\n<p>Move directly to the goal, no back and forth. Cost will be the same no matter which path you choose.<\/p>\n\n\n\n<p>ans = sum(rowCosts[y1+1~y2]) + sum(colCosts[x1+1~x2])<\/p>\n\n\n\n<p>Time complexity: O(m + n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int minCost(vector<int>& startPos, vector<int>& homePos, vector<int>& rowCosts, vector<int>& colCosts) {\n    int dx = homePos[1] > startPos[1] ? 1 : (homePos[1] < startPos[1] ? -1 : 0);\n    int dy = homePos[0] > startPos[0] ? 1 : (homePos[0] < startPos[0] ? -1 : 0);\n    int ans = 0;\n    while (homePos[1] != startPos[1]) ans += colCosts[startPos[1] += dx];\n    while (homePos[0] != startPos[0]) ans += rowCosts[startPos[0] += dy];    \n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an&nbsp;m x n&nbsp;grid, where&nbsp;(0, 0)&nbsp;is the top-left cell and&nbsp;(m &#8211; 1, n &#8211; 1)&nbsp;is the bottom-right cell. You are given an integer array&nbsp;startPos&nbsp;where&nbsp;startPos&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,179],"class_list":["post-8816","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8816"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8816\/revisions"}],"predecessor-version":[{"id":8819,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8816\/revisions\/8819"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8816"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}