当前位置:网站首页>Distance between bus stops: simple simulation problem
Distance between bus stops: simple simulation problem
2022-07-26 05:01:00 【Fat technology house】
Title Description
This is a LeetCode Upper 1184. The distance between bus stops , The difficulty is Simple .
Tag : 「 simulation 」
There are... On the circular bus route nnn Individual station , From 000 To n−1n - 1n−1 Number . We know the distance between each pair of adjacent bus stops ,distance[i]distance[i]distance[i] Indicates that the number is iii 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.
Copy code 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.
Copy code 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.
Copy code Tips :
- 1<=n <=1041 <= n <= 10^41<=n <=104
- distance.length==ndistance.length == ndistance.length==n
- 0<=start,destination<n0 <= start, destination < n0<=start,destination<n
- 0<=distance[i]<=1040 <= distance[i] <= 10^40<=distance[i]<=104
simulation
Simulate according to the meaning of the question .
use i and j The pointer representing forward and backward respectively ,a and b Calculate the total cost of the two methods respectively .
Java Code :
class Solution {
public int distanceBetweenBusStops(int[] dist, int s, int t) {
int n = dist.length, i = s, j = s, a = 0, b = 0;
while (i != t) {
a += dist[i];
if (++i == n) i = 0;
}
while (j != t) {
if (--j < 0) j = n - 1;
b += dist[j];
}
return Math.min(a, b);
}
}
Copy code TypeScript Code :
function distanceBetweenBusStops(dist: number[], s: number, t: number): number {
let n = dist.length, i = s, j = s, a = 0, b = 0
while (i != t) {
a += dist[i]
if (++i == n) i = 0
}
while (j != t) {
if (--j < 0) j = n - 1
b += dist[j]
}
return Math.min(a, b)
};
Copy code - Time complexity :O(n)O(n)O(n)
- Spatial complexity :O(1)O(1)O(1)

边栏推荐
- Spark Structured Streaming HelloWorld
- Use field parameters for report translation
- JVM第五讲:纵横数据如何应对洪峰推送
- 面试之请详细说下synchronized的实现原理以及相关的锁
- UE4 keyboard control switch light
- 常函数const的学习
- 公交站间的距离 : 简单模拟题
- 未来大气污染变化模拟
- Database startup message: ora-29702: error occurred in cluster group service
- Character function and string function (I)
猜你喜欢

C language - pointer one touch ※

快恢复二极管工作原理及使用

分布式ID的常用解决方案-一把拿下

Correspondence between IEC61131 data type and C data type

data warehouse

can 串口 can 232 can 485 串口转CANbus总线网关模块CAN232/485MB转换器CANCOM

推荐12个免费查找文献的学术网站,建议点赞、收藏!

Bsdiff and bspatch incremental updates

Customer service relationship management based on SQL net enterprise messenger enterprise communications

Rman-06031 cannot convert database keywords
随机推荐
阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
推荐12个免费查找文献的学术网站,建议点赞、收藏!
Soft exam review and plan
Two ways to create MySQL database
Spark Structured Streaming HelloWorld
What points should be paid attention to in the selection of project management system?
@Autowired注解的原理
Niuke-top101-bm32
【洛谷】P3919 【模板】可持久化线段树1(可持久化数组)
data warehouse
There was an unexpected error (type=method not allowed, status=405)
5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
Codeforces Round #807 (Div. 2)
[mathematical modeling] basic knowledge of MATLAB
分子骨架跃迁工具-DeLinker介绍
autocomplete禁止表单自动填充
[cloud native | 17] four network modes of container
解决 Incorrect string value: ‘\xF0\x9F\x98\xAD“,...‘ for column ‘commentContent‘ at row 1 报错
UE4 displays text when it is close to the object, and disappears when it is far away
2022 Henan Mengxin League game (3): Henan University L - synthetic game