{"id":5528,"date":"2019-09-07T21:12:57","date_gmt":"2019-09-08T04:12:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5528"},"modified":"2019-09-07T21:13:31","modified_gmt":"2019-09-08T04:13:31","slug":"leetcode-1184-distance-between-bus-stops","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1184-distance-between-bus-stops\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1184. Distance Between Bus Stops"},"content":{"rendered":"\n<p>A bus&nbsp;has&nbsp;<code>n<\/code>&nbsp;stops numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>&nbsp;that form&nbsp;a circle. We know the distance between all pairs of neighboring stops where&nbsp;<code>distance[i]<\/code>&nbsp;is the distance between the stops number&nbsp;<code>i<\/code>&nbsp;and&nbsp;<code>(i + 1) % n<\/code>.<\/p>\n\n\n\n<p>The bus goes along both directions&nbsp;i.e. clockwise and counterclockwise.<\/p>\n\n\n\n<p>Return the shortest distance between the given&nbsp;<code>start<\/code>&nbsp;and&nbsp;<code>destination<\/code>&nbsp;stops.<\/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\/2019\/09\/03\/untitled-diagram-1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> distance = [1,2,3,4], start = 0, destination = 1\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Distance between 0 and 1 is 1 or 9, minimum is 1.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/09\/03\/untitled-diagram-1-1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> distance = [1,2,3,4], start = 0, destination = 2\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Distance between 0 and 2 is 3 or 7, minimum is 3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2019\/09\/03\/untitled-diagram-1-2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> distance = [1,2,3,4], start = 0, destination = 3\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> Distance between 0 and 3 is 6 or 4, minimum is 4.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n&nbsp;&lt;= 10^4<\/code><\/li><li><code>distance.length == n<\/code><\/li><li><code>0 &lt;= start, destination &lt; n<\/code><\/li><li><code>0 &lt;= distance[i] &lt;= 10^4<\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: Summation<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>compute the total sum<\/li><li>compute the sum from s to d, c<\/li><li>ans = min(c, sum &#8211; c)<\/li><\/ol>\n\n\n\n<p>Time complexity: O(d-s)<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 distanceBetweenBusStops(vector<int>& ds, int s, int d) {    \n    if (s > d) swap(s, d);\n    int sum = accumulate(begin(ds), end(ds), 0);\n    int d1 = accumulate(begin(ds) + s, begin(ds) + d, 0);\n    return min(d1, sum - d1);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A bus&nbsp;has&nbsp;n&nbsp;stops numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1&nbsp;that form&nbsp;a circle. We know the distance between all pairs of neighboring stops where&nbsp;distance[i]&nbsp;is the distance between the stops number&nbsp;i&nbsp;and&nbsp;(i&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,77,62],"class_list":["post-5528","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-graph","tag-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5528","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=5528"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5528\/revisions"}],"predecessor-version":[{"id":5530,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5528\/revisions\/5530"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5528"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5528"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5528"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}