当前位置:网站首页>1184. Distance between bus stops: simple simulation problem
1184. Distance between bus stops: simple simulation problem
2022-07-24 11:37:00 【Gong Shui Sanye's Diary of writing questions】
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 Individual station , From To Number . We know the distance between each pair of adjacent bus stops , Indicates that the number is 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 :
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);
}
}
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)
};
Time complexity : Spatial complexity :
Last
This is us. 「 Brush through LeetCode」 The first of the series No.1184 piece , The series begins with 2021/01/01, As of the start date LeetCode I have in common 1916 questions , Part of it is a locked question , We will finish all the questions without lock first .
In this series , In addition to explaining the idea of solving problems , And give the most concise code possible . If the general solution is involved, there will be corresponding code templates .
In order to facilitate the students to debug and submit code on the computer , I've built a warehouse :https://github.com/SharingSource/LogicStack-LeetCode .
In the warehouse address , You can see the links to the series 、 The corresponding code of the series 、LeetCode Links to the original problem and other preferred solutions .
More, more, more popular 「 written examination / interview 」 Relevant information can be found in the beautifully arranged Gather new bases
边栏推荐
- 【C】 Recursive and non recursive writing of binary tree traversal
- Blue Bridge Cup provincial match training camp - Calculation of date
- Code of login page
- How to choose sentinel vs. hystrix current limiting?
- 一周精彩内容分享(第13期)
- Grep actually uses ps/netstat/sort
- Fifty lectures of Euler (I)
- 运算放大器 —— 快速复苏笔记[壹](参数篇)
- 离散分布常用公式及应用场景
- What is cloud native? Why is cloud native technology so popular?
猜你喜欢

Pytorch learning -- using gradient descent method to realize univariate linear regression

Nodejs ctf 基础

[TA frost wolf umay - "hundred people plan] Figure 3.3 surface subdivision and geometric shader large-scale grass rendering
什么是云原生,云原生技术为什么这么火?

Share the typora tool

Video playback | how to become an excellent reviewer of international journals in the field of Geoscience and ecology?

Jmeter-If控制器

黑马瑞吉外卖之员工信息分页查询

字符串——344.反转字符串

HDU5667 Sequence
随机推荐
Hcip OSPF interface network type experiment day 4
Semaphore details
Easy to use example
Linked list - Sword finger offer interview question 02.07. linked list intersection
Stream stream
The difference between YPbPr and YCbCr
【10】团队协作和跨团队协作
MySQL creates partition tables and automatically partitions them by day
How to use SSH and SFTP protocols at home
Basic syntax of MySQL DDL and DML and DQL
Performance test summary (I) -- basic theory
Fifty lectures of Euler (I)
MOS管 —— 快速复苏应用笔记(壹)[原理篇]
有关并行的两个重要定律
Operational amplifier - Notes on rapid recovery [II] (application)
String -- 344. Reverse string
2022,软测人的平均薪资,看完我瞬间凉了...
Cgo+gsoap+onvif learning summary: 9. Go and C conduct socket communication and onvif protocol processing
使用Prometheus+Grafana实时监控服务器性能
什么是云原生,云原生技术为什么这么火?