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

Eureka usage record

Microsoft azure and Analysys jointly released the report "Enterprise Cloud native platform driven digital transformation"

Can't delete the blank page in word? How to operate?

JS 将伪数组转换成数组

Pytorch visualization

More accurate and efficient segmentation of organs-at-risk in radiotherapy with Convolutional Neural

【ROS进阶篇】第九讲 URDF的编程优化Xacro使用

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

技术管理杂谈

3.2.1 什么是机器学习?
随机推荐
[fluent -- example] case 1: comprehensive example of basic components and layout components
scrapy 爬虫框架简介
mysql实现一张表数据插入另一张表
【Rust】引用和借用,字符串切片 (slice) 类型 (&str)——Rust语言基础12
cmake 学习使用笔记(二)库的生成与使用
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
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
mysql的表分区
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
想要做好软件测试,可以先了解AST、SCA和渗透测试
【8】 Clever use of color finder
Pytorch main module
Cmake learning notes (II) generation and use of Library
PyTorch的生态简介
perf 性能调试
Azure Devops (XIV) use azure's private nuget warehouse
想要做好软件测试,可以先了解AST、SCA和渗透测试
请问一下,使用数据集成从postgreSQL导数据到Mysql数据库,有部分数据的字段中出现emoj
Implementation of recommendation system collaborative filtering in spark
【3】 DEM mountain shadow effect