当前位置:网站首页>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
边栏推荐
- 水博士2
- 弹性盒子(Flex Box)详解
- Implementation of recommendation system collaborative filtering in spark
- 【ROS进阶篇】第九讲 URDF的编程优化Xacro使用
- mysql有 flush privileges 吗
- PyTorch的生态简介
- Communication bus protocol I: UART
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- Does MySQL have flush privileges
- WPF项目入门1-简单登录页面的设计和开发
猜你喜欢

After having a meal with trump, I wrote this article

Interviewer: "classmate, have you ever done a real landing project?"

【C语言进阶】动态内存管理

Build a series of vision transformer practices, and finally meet, Timm library!

Use of hystrix

scrapy爬虫爬取动态网站

搭建Vision Transformer系列实践,终于见面了,Timm库!

【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12

Resttemplate and ribbon are easy to use

Dr. water 2
随机推荐
Zuul gateway use
1.1.1 欢迎来到机器学习
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
Fiddler packet capturing app
Unexpected rollback exception analysis and transaction propagation strategy for nested transactions
Use of hystrix
【6】 Map box settings
Introduction to the scratch crawler framework
微软Azure和易观分析联合发布《企业级云原生平台驱动数字化转型》报告
scrapy爬虫爬取动态网站
使用TensorBoard可视化训练过程
启牛开的证券账户安全吗?是怎么开账户的
Pytorch main module
Fiddler抓包APP
Ecological profile of pytorch
Pytorch environment configuration and basic knowledge
What does the software testing process include? What are the test methods?
循环创建目录与子目录
阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?
【Flutter -- 实例】案例一:基础组件 & 布局组件综合实例