当前位置:网站首页>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
边栏推荐
- 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
- If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
- keepalived实现mysql的高可用
- 3.2.1 什么是机器学习?
- 【Flutter -- 实例】案例一:基础组件 & 布局组件综合实例
- scrapy 设置随机的user_agent
- Atomic atomic class
- Pytorch advanced training skills
- Implementation of recommendation system collaborative filtering in spark
- Eureka usage record
猜你喜欢

第一个scrapy爬虫

JS 将伪数组转换成数组

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

Location analysis of recording an online deadlock

If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing

Eureka usage record

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

Fiddler抓包APP

WPF项目入门1-简单登录页面的设计和开发

LeetCode 0133. 克隆图
随机推荐
Azure Devops(十四) 使用Azure的私有Nuget仓库
2022河南萌新联赛第(三)场:河南大学 I - 旅行
【十一】矢量、栅格数据图例制作以及调整
Fiddler抓包APP
【七】图层显示和标注
mysql实现一张表数据插入另一张表
PyTorch的生态简介
Kyligence was selected into Gartner 2022 data management technology maturity curve report
【8】 Clever use of color finder
第一个scrapy爬虫
【高并发】通过源码深度分析线程池中Worker线程的执行流程
【五】页面和打印设置
我在源头SQLServer里面登记绝对删除的数据,传到MaxComputer,在数据清洗的时候写绝对
Feign use
A method to prevent SYN flooding attacks -- syn cookies
keepalived实现mysql的高可用
Pytorch main module
[ROS advanced chapter] Lecture 9 programming optimization of URDF and use of xacro
Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
Script set random user_ agent