当前位置:网站首页>LeetCode 1184. 公交站间的距离
LeetCode 1184. 公交站间的距离
2022-07-25 11:56:00 【Tisfy】
【LetMeFly】1184.公交站间的距离
力扣题目链接:https://leetcode.cn/problems/distance-between-bus-stops/
环形公交路线上有 n 个站,按次序从 0 到 n - 1 进行编号。我们已知每一对相邻公交站之间的距离,distance[i] 表示编号为 i 的车站和编号为 (i + 1) % n 的车站之间的距离。
环线上的公交车都可以按顺时针和逆时针的方向行驶。
返回乘客从出发点 start 到目的地 destination 之间的最短距离。
示例 1:

输入:distance = [1,2,3,4], start = 0, destination = 1 输出:1 解释:公交站 0 和 1 之间的距离是 1 或 9,最小值是 1。
示例 2:

输入:distance = [1,2,3,4], start = 0, destination = 2 输出:3 解释:公交站 0 和 2 之间的距离是 3 或 7,最小值是 3。
示例 3:

输入:distance = [1,2,3,4], start = 0, destination = 3 输出:4 解释:公交站 0 和 3 之间的距离是 6 或 4,最小值是 4。
提示:
1 <= n <= 10^4distance.length == n0 <= start, destination < n0 <= distance[i] <= 10^4
方法一:模拟
既然公交车是双向的,那么不如计算一下“从 s t a r t start start和 d e s t i n a t i o n destination destination中编号较小的那个到编号较大的那个 的距离” s 1 s1 s1
然后计算一下一圈的总距离 s s s
那么乘坐另一方向的公交车的距离就是 s − s 1 s-s1 s−s1
返回 s 1 s1 s1和 s − s 1 s-s1 s−s1中较小的那个即可
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是公交车站的个数
- 空间复杂度 O ( 1 ) O(1) O(1)
AC代码
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);
}
};

同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125960214
边栏推荐
- Keeping MySQL highly available
- 2.1.2 application of machine learning
- 【7】 Layer display and annotation
- Plus SBOM: assembly line BOM pbom
- Spirng @Conditional 条件注解的使用
- From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"
- Client open download, welcome to try
- 1.1.1 welcome to machine learning
- flinkcdc可以一起导mongodb数据库中的多张表吗?
- 面试官:“同学,你做过真实落地项目吗?”
猜你喜欢

From cloud native to intelligent, in-depth interpretation of the industry's first "best practice map of live video technology"

MySQL练习二

Azure Devops(十四) 使用Azure的私有Nuget仓库

Communication bus protocol I: UART
![[fluent -- example] case 1: comprehensive example of basic components and layout components](/img/d5/2392d9cb8550aa2692c8b41303d507.png)
[fluent -- example] case 1: comprehensive example of basic components and layout components

Brpc source code analysis (IV) -- bthread mechanism

Unexpected rollback exception analysis and transaction propagation strategy for nested transactions

2022.07.24(LC_6125_相等行列对)

Jenkins配置流水线

水博士2
随机推荐
如何从远程访问 DMS数据库?IP地址是啥?用户名是啥?
Fault tolerant mechanism record
After having a meal with trump, I wrote this article
Jenkins配置流水线
搭建Vision Transformer系列实践,终于见面了,Timm库!
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
【ROS进阶篇】第九讲 URDF的编程优化Xacro使用
Plus版SBOM:流水线物料清单PBOM
919. 完全二叉树插入器 : 简单 BFS 运用题
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
Synergetic process
【11】 Production and adjustment of vector and grid data Legends
R language uses LM function to build multiple linear regression model, step function to build forward stepwise regression model to screen the best subset of prediction variables, and scope parameter t
Pairwise comparison of whether the mean values between R language groups are the same: pairwise hypothesis test of the mean values of multiple grouped data is performed using pairwise.t.test function
【Flutter -- 布局】层叠布局(Stack和Positioned)
Musk's "eternal soul": half hype, half flicker
[dark horse morning post] eBay announced its shutdown after 23 years of operation; Wei Lai throws an olive branch to Volkswagen CEO; Huawei's talented youth once gave up their annual salary of 3.6 mil
循环创建目录与子目录
面试官:“同学,你做过真实落地项目吗?”
Communication bus protocol I: UART