当前位置:网站首页>Leetcode 1184. distance between bus stops
Leetcode 1184. distance between bus stops
2022-07-25 12:36:00 【Tisfy】
【LetMeFly】1184. The distance between bus stops
Force button topic link :https://leetcode.cn/problems/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
Method 1 : simulation
Since the bus is two-way , Then why don't you calculate “ from s t a r t start start and d e s t i n a t i o n destination destination The one with the smaller number in the middle to the one with the larger number Distance of ” s 1 s1 s1
Then calculate the total distance of a lap s s s
Then the distance to take the bus in the other direction is s − s 1 s-s1 s−s1
return s 1 s1 s1 and s − s 1 s-s1 s−s1 The smaller one is ok
- Time complexity O ( n ) O(n) O(n), among n n n It's the number of bus stops
- Spatial complexity O ( 1 ) O(1) O(1)
AC Code
C++
class Solution {
public:
int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
if (start > destination) swap(start, destination);
int s1 = 0;
for (int i = start; i < destination; i++) {
s1 += distance[i];
}
int s = 0;
for (int i = 0; i < distance.size(); i++) {
s += distance[i];
}
return min(s1, s - s1);
}
};

Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/125960214
边栏推荐
猜你喜欢

Azure Devops (XIV) use azure's private nuget warehouse

Interviewer: "classmate, have you ever done a real landing project?"
![[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro](/img/a2/9b676d0f1b33cc7d413cee6c52d76d.png)
[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro

Microsoft azure and Analysys jointly released the report "Enterprise Cloud native platform driven digital transformation"

Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network

3.2.1 what is machine learning?

防范SYN洪泛攻击的方法 -- SYN cookie

Jenkins configuration pipeline

PyTorch可视化

想要做好软件测试,可以先了解AST、SCA和渗透测试
随机推荐
循环创建目录与子目录
Monit installation and use
PyTorch进阶训练技巧
pytorch环境配置及基础知识
Resttemplate and ribbon are easy to use
Microsoft azure and Analysys jointly released the report "Enterprise Cloud native platform driven digital transformation"
Spirng @Conditional 条件注解的使用
Feign use
【五】页面和打印设置
R language ggplot2 visualization: visualize the scatter diagram, add text labels to some data points in the scatter diagram, and use geom of ggrep package_ text_ The repl function avoids overlapping l
2.1.2 机器学习的应用
R language Visual scatter diagram, geom using ggrep package_ text_ The rep function avoids overlapping labels between data points (set the min.segment.length parameter to inf and do not add label segm
Azure Devops (XIV) use azure's private nuget warehouse
Ecological profile of pytorch
flinkcdc可以一起导mongodb数据库中的多张表吗?
Create directories and subdirectories circularly
水博士2
LeetCode 0133. 克隆图
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
【3】 DEM mountain shadow effect