当前位置:网站首页>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
边栏推荐
猜你喜欢

2022.07.24(LC_6124_第一个出现两次的字母)

3.2.1 什么是机器学习?

Basic concepts of NLP 1

scrapy 爬虫框架简介
软件测试流程包括哪些内容?测试方法有哪些?

919. 完全二叉树插入器 : 简单 BFS 运用题

PyTorch可视化

Interviewer: "classmate, have you ever done a real landing project?"
![[micro service ~sentinel] sentinel degradation, current limiting, fusing](/img/60/448c5f40af4c0937814c243bd7cb04.png)
[micro service ~sentinel] sentinel degradation, current limiting, fusing

【ROS进阶篇】第九讲 URDF的编程优化Xacro使用
随机推荐
【2】 Grid data display stretch ribbon (take DEM data as an example)
keepalived实现mysql的高可用
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
Pytorch visualization
R language ggpubr package ggarrange function combines multiple images and annotates_ Figure function adds annotation, annotation and annotation information for the combined image, adds image labels fo
MySQL implements inserting data from one table into another table
Fiddler packet capturing app
【9】 Coordinate grid addition and adjustment
循环创建目录与子目录
Add a little surprise to life and be a prototype designer of creative life -- sharing with X contestants in the programming challenge
Interviewer: "classmate, have you ever done a real landing project?"
How to access DMS database remotely? What is the IP address? What is the user name?
【七】图层显示和标注
1.1.1 欢迎来到机器学习
NLP knowledge - pytorch, back propagation, some small pieces of notes for predictive tasks
【10】 Scale bar addition and adjustment
和特朗普吃了顿饭后写下了这篇文章
The first scratch crawler
Brpc source code analysis (IV) -- bthread mechanism
PyTorch进阶训练技巧