当前位置:网站首页>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)

边栏推荐
- Use field parameters for report translation
- 你对“happen-before原则”的理解可能是错的?
- JVM第二讲:类加载机制
- 有ggjj看看这个问题没,是否缓存导致跨域问题?
- Tonight! Stonedb is officially open source. Please check this strategy~
- Torch slice maintenance
- clock_ gettime
- Principle of image nonlocal mean filtering
- Working principle and application of fast recovery diode
- There was an unexpected error (type=method not allowed, status=405)
猜你喜欢

Google Emoji guessing game helps parents guide their children to surf the Internet safely

Stm32fsmc extended SRAM

Study of const of constant function

AQS唤醒线程的时候为什么从后向前遍历,我懂了

BigDecimal 的 4 个坑,你踩过几个?

Icml2022 | imitation learning by evaluating the professional knowledge of the presenter

mysql函数汇总之日期和时间函数

Alibaba cloud industrial vision intelligent engineer ACP certification - Preparation

Several maturity levels of using MES management system

Seata两阶段提交AT详解
随机推荐
The pit of history can only be filled up as far as possible
Textfield and password input box that are more flexible and easy to use in compose
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly 100 times the improvement of analysis performance
Good at C (summer vacation daily question 6)
webassembly 01基本资料
Have you known several distribution methods of NFT? What are the advantages and disadvantages of different distribution methods?
Embedded practice -- CPU utilization statistics based on rt1170 FreeRTOS (24)
What points should be paid attention to in the selection of project management system?
Alibaba cloud industrial vision intelligent engineer ACP certification - Preparation
What are the well-known to-do apps at home and abroad
[semantic segmentation] 2018-deeplabv3+ ECCV
What is the difference between asynchronous and synchronous transmission signals (electronic hardware)
List转换为tree-项目真实使用
Unnamed Article 33
【语义分割】2018-DeeplabV3+ ECCV
columns in GROUP BY clause; this is incompatible with sql_ mode=only_ full_ group_ By mysql8.0 solution
Mysql主从同步及主从同步延迟解决方案
C language -- string function, memory function collection and Simulation Implementation
Redis solves the problem of oversold inventory
【洛谷】P3919 【模板】可持久化线段树1(可持久化数组)