LeetCode 1184. Distance Between Bus Stops (Easy)

A bus has n stops numbered from 0 to n - 1 that form a circle. We know the distance between all pairs of neighboring stops where distance[i] is the distance between the stops number i and (i + 1) % n.

The bus goes along both directions i.e. clockwise and counterclockwise.

Return the shortest distance between the given start and destination stops.

Example 1:

Example 1

Input: distance = [1,2,3,4], start = 0, destination = 1
Output: 1
Explanation: Distance between 0 and 1 is 1 or 9, minimum is 1.

Example 2:

Example 2

Input: distance = [1,2,3,4], start = 0, destination = 2
Output: 3
Explanation: Distance between 0 and 2 is 3 or 7, minimum is 3.

Example 3:

Example 3

Constraints:

  • 1 <= n <= 10^4
  • distance.length == n
  • 0 <= start, destination < n
  • 0 <= distance[i] <= 10^4

Solution:

class Solution:
    def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
        if destination < start:
            start, destination = destination, start
        clockwise = sum(distance[start : destination])
        cnt_clockwise = sum(distance) - clockwise
        return min(clockwise, cnt_clockwise) 

Explanation:
For this question, we just need to find the sum of distances of stops between start and destination, both clockwisely or counterclockwisely. Then we pick the minimum of these to as our answer. Just keep in mind that start could be greater than destination, so the clockwise and counterclockwise distance will be swapped in this case. Since we just need to find the minimum and don't care about directions(i.e. we don't care whether the minimal is from clockwise or counterclockwise), we can swap the start and destination at the beginning to make sure start is always less than destination, thus making our life easier.
本題我們只需要找到順時(shí)針或逆時(shí)針之間起點(diǎn)和終點(diǎn)之間的驮髅睿靠點(diǎn)距離之和即可旋炒。然后霎桅,我們選擇其中的最小值作為答案旅择。需要注意的是 start 可能大于destination,因此在這種情況下预麸,順時(shí)針和逆時(shí)針距離將完全相反瞪浸。由于我們只需要找到兩者最小值,而不關(guān)心具體方向(我們不關(guān)心這個(gè)最小值是來自順時(shí)針或者逆時(shí)針)吏祸,因此我們完全可以在一開始時(shí)交換起點(diǎn)和終點(diǎn)对蒲,使解題更輕松。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末贡翘,一起剝皮案震驚了整個(gè)濱河市蹈矮,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鸣驱,老刑警劉巖泛鸟,帶你破解...
    沈念sama閱讀 218,122評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異踊东,居然都是意外死亡北滥,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評論 3 395
  • 文/潘曉璐 我一進(jìn)店門闸翅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來再芋,“玉大人,你說我怎么就攤上這事缎脾∽N牛” “怎么了?”我有些...
    開封第一講書人閱讀 164,491評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長联喘。 經(jīng)常有香客問我华蜒,道長,這世上最難降的妖魔是什么豁遭? 我笑而不...
    開封第一講書人閱讀 58,636評論 1 293
  • 正文 為了忘掉前任叭喜,我火速辦了婚禮,結(jié)果婚禮上蓖谢,老公的妹妹穿的比我還像新娘捂蕴。我一直安慰自己,他們只是感情好闪幽,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評論 6 392
  • 文/花漫 我一把揭開白布啥辨。 她就那樣靜靜地躺著,像睡著了一般盯腌。 火紅的嫁衣襯著肌膚如雪溉知。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評論 1 305
  • 那天腕够,我揣著相機(jī)與錄音级乍,去河邊找鬼。 笑死帚湘,一個(gè)胖子當(dāng)著我的面吹牛玫荣,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播大诸,決...
    沈念sama閱讀 40,292評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼捅厂,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了资柔?” 一聲冷哼從身側(cè)響起恒傻,我...
    開封第一講書人閱讀 39,211評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎建邓,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體睁枕,經(jīng)...
    沈念sama閱讀 45,655評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡官边,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了外遇。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片注簿。...
    茶點(diǎn)故事閱讀 39,965評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖跳仿,靈堂內(nèi)的尸體忽然破棺而出诡渴,到底是詐尸還是另有隱情,我是刑警寧澤菲语,帶...
    沈念sama閱讀 35,684評論 5 347
  • 正文 年R本政府宣布妄辩,位于F島的核電站惑灵,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏眼耀。R本人自食惡果不足惜英支,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望哮伟。 院中可真熱鬧干花,春花似錦、人聲如沸楞黄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鬼廓。三九已至肿仑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間桑阶,已是汗流浹背柏副。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蚣录,地道東北人割择。 一個(gè)月前我還...
    沈念sama閱讀 48,126評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像萎河,于是被迫代替她去往敵國和親荔泳。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評論 2 355

推薦閱讀更多精彩內(nèi)容