当前位置:网站首页>[leetcode] 1184. Distance between bus stops
[leetcode] 1184. Distance between bus stops
2022-07-24 20:04:00 【pass night】
subject
1184. The distance between bus stops
There are... On the circular bus route n Individual station , From 0 To n - 1 Number . We know the distance between each pair of adjacent bus stops ,distance[i] Indicates that the number is i The station and number are (i + 1) % n The distance between the stations .
The buses on the loop line can travel clockwise and counterclockwise .
Return passengers from the starting point start Destination destination The shortest distance between .
Example 1:

Input :distance = [1,2,3,4], start = 0, destination = 1
Output :1
explain : Bus stop 0 and 1 The distance between them is 1 or 9, The minimum is 1.
Example 2:

Input :distance = [1,2,3,4], start = 0, destination = 2
Output :3
explain : Bus stop 0 and 2 The distance between them is 3 or 7, The minimum is 3.
Example 3:

Input :distance = [1,2,3,4], start = 0, destination = 3
Output :4
explain : Bus stop 0 and 3 The distance between them is 6 or 4, The minimum is 4.
Tips :
1 <= n <= 10^4distance.length == n0 <= start, destination < n0 <= distance[i] <= 10^4
Ideas
- It is equivalent to the minimum value of the sum of clockwise path and counterclockwise path
Code
class Solution:
def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
if start>destination: start,destination = destination,start
return min(sum(distance[start:destination]), sum(distance[destination:]) + sum(distance[:start]) )
Complexity
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- C language implementation of raii
- Jedi survive and eat chicken F12 screenshot save path reference
- Mass modify attribute values in objects in JS
- How to select the shelling tool?
- Create a life cycle aware MVP architecture
- Sword finger offer 47. the maximum value of gifts
- How to integrate Kata in kubernetes cluster
- Interface component devaxpress asp Net v22.1 - new office 365 dark theme
- Excuse me: is Flink 1.14.5 compatible with MySQL CDC 2.1.0
- The difference between delete, truncate and drop in MySQL
猜你喜欢

How to export map files tutorial

原反补及大小端

Machine learning_ Softmax function (multi classification problem)

Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack
![[face to face experience of school recruitment] 8 real questions of pointer interview. Come and test how many you have mastered.](/img/2c/e687b224285aeee66dacace6331161.png)
[face to face experience of school recruitment] 8 real questions of pointer interview. Come and test how many you have mastered.

BGP - border gateway protocol

Conversion between VC string and timestamp

Read the registry through the ATL library clegkey (simple and convenient)

Create a life cycle aware MVP architecture

Modbus communication protocol specification (Chinese) sharing
随机推荐
Covid-19-20 - basic method of network segmentation based on vnet3d
Analysis and Simulation of strlen function
Jedi survive and eat chicken F12 screenshot save path reference
Anaconda installs labelimg (super simple and available)
clip:learning transferable visual models from natural language supervision
Maya coffee machine modeling
Risk control system, implemented by flink+clickhouse!
Solve the problem that gd32f207 serial port can receive but send 00
Sword finger offer 53 - I. find the number I in the sorted array
strlen函数剖析和模拟实现
Microservice architecture | service monitoring and isolation - [sentinel] TBC
Sword finger offer 40. minimum number of K
Wechat applet -that.setdata ({}) set complex field data
Hold the C pointer
Hide the middle digit of ID number
871. Sum of divisors
MySQL8.0学习记录19 - 页区段与表空间
Pure C implementation -------- Nicolas theorem
02 | environment preparation: how to install and configure a basic PHP development environment under windows?
Detailed explanation of ELF format (I)