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

Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?

【Flutter -- 实例】案例一:基础组件 & 布局组件综合实例

Kyligence 入选 Gartner 2022 数据管理技术成熟度曲线报告

919. Complete binary tree inserter: simple BFS application problem

技术管理杂谈

PyTorch可视化

cmake 学习使用笔记(二)库的生成与使用

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

第一个scrapy爬虫

LeetCode 0133. 克隆图
随机推荐
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
2022.07.24(LC_6124_第一个出现两次的字母)
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
PyTorch的生态简介
2.1.2 机器学习的应用
Ecological profile of pytorch
Pytorch visualization
flinkcdc可以一起导mongodb数据库中的多张表吗?
Keeping MySQL highly available
919. 完全二叉树插入器 : 简单 BFS 运用题
【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12
919. Complete binary tree inserter: simple BFS application problem
Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?
启牛开的证券账户安全吗?是怎么开账户的
使用TensorBoard可视化训练过程
3.2.1 什么是机器学习?
Spirng @Conditional 条件注解的使用
PyTorch主要模块
SSTI 模板注入漏洞总结之[BJDCTF2020]Cookie is so stable