当前位置:网站首页>Li Kou 1184. Distance between bus stops
Li Kou 1184. Distance between bus stops
2022-07-24 22:28:00 【Ruthless young Fisherman】
subject
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

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.

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.

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.
source : Power button (LeetCode)
link :https://leetcode.cn/problems/distance-between-bus-stops
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Method 1: simulation
Java Realization
class Solution {
public int distanceBetweenBusStops(int[] distance, int start, int destination) {
if (start > destination) {
int tmp = start;
start = destination;
destination = tmp;
}
int sum = 0, n = distance.length, flag = 0;
for (int i = 0; i < n; i++) {
sum += distance[i];
if (i >= start && i < destination) flag += distance[i];
}
return Math.min(flag, sum - flag);
}
}

边栏推荐
- Leetcode 101. symmetric binary tree
- [postgraduate entrance examination English vocabulary training camp] day 11 - offer, form, maintain, critical
- Local data enhancement method of icml2022 | graph neural network
- What is a video content recommendation engine?
- Go+ language
- Push information to wechat through enterprise wechat self built application
- 并查集结构
- SQL语言的通用语法及分类(二)
- Projection regularization of line point set in PCL point cloud processing (56)
- My love lesson 2 - remove webpage pop-up
猜你喜欢

Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises

Web3安全 Go+Security

CAD copy commands

CAD break command

Gradle learning - gradle advanced instructions

【ICML2022】气候变化与机器学习:机遇、挑战与考虑,121页ppt

Dialogue with celebrities: where are the opportunities and challenges in the second half when brands gather at the shuzang track?

Im instant messaging develops ten million level concurrent long connection Gateway Technology

力扣 1184. 公交站间的距离

Gradle learning - integration of gradle and idea
随机推荐
力扣 1184. 公交站间的距离
IndexTree
Moving least squares fitting experiment of PCL point cloud processing (62)
Leetcode 101. symmetric binary tree
QT学习之VS创建QT项目显示未将对象引用设置到对象的实例
VScode默认输出到调试控制台如何调整到终端以及两者中的乱码问题
Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises
【ICML2022】气候变化与机器学习:机遇、挑战与考虑,121页ppt
《元宇宙2086》单日销量超《三体》 夺得京东图书单日科幻榜冠军
Cell专刊|AI在蛋白结构、精准医疗、抗体疗法[综述]等的应用与未来预测
Dialogue with celebrities: where are the opportunities and challenges in the second half when brands gather at the shuzang track?
由斐波那契数列引述到矩阵快速幂技巧
Pyqt5 uses qfile and qdatastream to read and write binary files
工业物联网中的时序数据
VC prompts to recompile every time you press F5 to run
Poj2308 continuously look at dfs+bfs+ optimization
GEE - 数据集介绍MCD12Q1
EL & JSTL: JSTL summary
Projection regularization of line point set in PCL point cloud processing (56)
Sensor experiment - 485 air temperature and humidity