{"id":5834,"date":"2019-11-23T23:37:04","date_gmt":"2019-11-24T07:37:04","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5834"},"modified":"2019-11-23T23:37:43","modified_gmt":"2019-11-24T07:37:43","slug":"leetcode-1266-minimum-time-visiting-all-points","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1266-minimum-time-visiting-all-points\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1266. Minimum Time Visiting All Points"},"content":{"rendered":"\n<p>On a plane there are&nbsp;<code>n<\/code>&nbsp;points with integer coordinates&nbsp;<code>points[i] = [xi, yi]<\/code>. Your task is to find the minimum time in seconds to visit all points.<\/p>\n\n\n\n<p>You can move according to the next rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>In one second always you can either move vertically, horizontally by one unit or diagonally (it means to move one unit vertically and one unit horizontally in one second).<\/li><li>You have to visit the points in the same order as they appear in the array.<\/li><\/ul>\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\/2019\/11\/14\/1626_example_1.PNG\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> points = [[1,1],[3,4],[-1,0]]\n<strong>Output:<\/strong> 7\n<strong>Explanation: <\/strong>One optimal path is <strong>[1,1]<\/strong> -&gt; [2,2] -&gt; [3,3] -&gt; <strong>[3,4] <\/strong>-&gt; [2,3] -&gt; [1,2] -&gt; [0,1] -&gt; <strong>[-1,0]<\/strong>   \nTime from [1,1] to [3,4] = 3 seconds \nTime from [3,4] to [-1,0] = 4 seconds\nTotal time = 7 seconds<\/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> points = [[3,2],[-2,2]]\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>points.length == n<\/code><\/li><li><code>1 &lt;= n&nbsp;&lt;= 100<\/code><\/li><li><code>points[i].length == 2<\/code><\/li><li><code>-1000&nbsp;&lt;= points[i][0], points[i][1]&nbsp;&lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: Geometry + Greedy<\/strong><br><br>dx = abs(x1 &#8211; x2)<br>dy = abs(y1 &#8211; y2)<br><br>go diagonally first for min(dx, dy) steps, and then go straight line for abs(dx &#8211; dy) steps. <\/p>\n\n\n\n<p>Time complexity: O(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 minTimeToVisitAllPoints(vector<vector<int>>& points) {\n    int ans = 0;\n    for (size_t i = 1; i < points.size(); ++i) {\n      int dx = abs(points[i - 1][0] - points[i][0]);\n      int dy = abs(points[i - 1][1] - points[i][1]);\n      ans += min(dx, dy) + abs(dx - dy);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>On a plane there are&nbsp;n&nbsp;points with integer coordinates&nbsp;points[i] = [xi, yi]. Your task is to find the minimum time in seconds to visit all points.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,284,88,31],"class_list":["post-5834","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-geometry","tag-greedy","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5834","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=5834"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5834\/revisions"}],"predecessor-version":[{"id":5836,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5834\/revisions\/5836"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}