当前位置:网站首页>公交站间的距离 : 简单模拟题
公交站间的距离 : 简单模拟题
2022-07-26 04:58:00 【肥肥技术宅】
题目描述
这是 LeetCode 上的 1184. 公交站间的距离 ,难度为 简单。
Tag : 「模拟」
环形公交路线上有 nnn 个站,按次序从 000 到 n−1n - 1n−1 进行编号。我们已知每一对相邻公交站之间的距离,distance[i]distance[i]distance[i] 表示编号为 iii 的车站和编号为 (i + 1) % n 的车站之间的距离。
环线上的公交车都可以按顺时针和逆时针的方向行驶。
返回乘客从出发点 start 到目的地 destination 之间的最短距离。
示例 1:

输入:distance = [1,2,3,4], start = 0, destination = 1
输出:1
解释:公交站 0 和 1 之间的距离是 1 或 9,最小值是 1。
复制代码示例 2:

输入:distance = [1,2,3,4], start = 0, destination = 2
输出:3
解释:公交站 0 和 2 之间的距离是 3 或 7,最小值是 3。
复制代码示例 3:

输入:distance = [1,2,3,4], start = 0, destination = 3
输出:4
解释:公交站 0 和 3 之间的距离是 6 或 4,最小值是 4。
复制代码提示:
- 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
模拟
根据题意进行模拟即可。
用 i 和 j 分别代表往前和往后走的指针,a 和 b 分别统计两种走法的总成本。
Java 代码:
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);
}
}
复制代码TypeScript 代码:
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)
};
复制代码- 时间复杂度:O(n)O(n)O(n)
- 空间复杂度:O(1)O(1)O(1)

边栏推荐
- Sliding window -- leetcode solution
- Ffmpeg video coding
- [300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)
- JVM第六讲:线上环境 FGC 频繁,如何解决?
- Add and modify the verification logic, and use -validation- to complete the group verification
- Correspondence between IEC61131 data type and C data type
- 创建MySQL数据库的两种方式
- Whether the SQL that fails to execute MySQL is counted into the slow query?
- 计算离散点的曲率(matlab)
- 擅长C(暑假每日一题 6)
猜你喜欢

What points should be paid attention to in the selection of project management system?

时代潮流-云原生数据库的崛起

Recursive implementation of exponential enumeration

计算离散点的曲率(matlab)

Briefly describe the application fields of WMS warehouse management system

Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!

分子骨架跃迁工具-DeLinker介绍

Ffmpeg video coding
![[semantic segmentation] 2018-deeplabv3+ ECCV](/img/c9/d1e2d7e63df8db2a7fa2bde31b10f7.png)
[semantic segmentation] 2018-deeplabv3+ ECCV

Axi protocol (5): burst mechanism of Axi protocol
随机推荐
To study the trend of open source and gain insight into the future of the industry, stonedb community and the China Academy of communications and communications released the Research Report on the dev
[uoj 429] runs (inclusive) + a little record about Lyndon tree and its application
Several maturity levels of using MES management system
时代潮流-云原生数据库的崛起
AXI协议(4):AXI通道上的信号
The importance of supporting horizontal expansion of time series database
How to connect tdengine through idea database management tool?
9 best project set management tools
Ffmpeg video coding
Offline installation of idea plug-in (continuous update)
[cloud native | 17] four network modes of container
Stm32fsmc extended SRAM
Molecular skeleton transition tool -delinker introduction
ES6 modularization +commonjs
AXI协议(5):AXI协议的burst机制
你对“happen-before原则”的理解可能是错的?
2022 Henan Mengxin League game (3): Henan University J - magic number
2022 Henan Mengxin League game (3): Henan University a - corn cannon
有ggjj看看这个问题没,是否缓存导致跨域问题?
Excel VBA:将多个工作表保存为新文件