当前位置:网站首页>LeetCode 1560. The sector with the most passes on the circular track
LeetCode 1560. The sector with the most passes on the circular track
2022-07-06 16:42:00 【Daylight629】
1560. The most frequent sector on a circular track
Give you an integer n And an array of integers rounds . There is a circular track made up of n Sectors make up , Sector number from 1 To n . Now a marathon will be held on this track , The marathon is run by m There are three stages . among , The first i The next stage will be from sector rounds[i - 1] Start , To sector rounds[i] end . for instance , The first 1 Stage from rounds[0] Start , To rounds[1] end .
Please return the sectors that have passed the most times in the form of array , By sector number Ascending array .
Be careful , The track forms a circle counterclockwise in ascending order of sector number ( See the first example ).
Example 1:

Input :n = 4, rounds = [1,3,1,2]
Output :[1,2]
explain : This marathon starts from 1 Start . The order of passing through each sector is as follows :
1 --> 2 --> 3( Stage 1 end )--> 4 --> 1( Stage 2 end )--> 2( Stage 3 end , That is, the end of the Marathon )
among , A sector 1 and 2 Twice , They are the two sectors that have passed the most times . A sector 3 and 4 Only once .
Example 2:
Input :n = 2, rounds = [2,1,2,1,2,1,2,1,2]
Output :[2]
Example 3:
Input :n = 7, rounds = [1,3,5,7]
Output :[1,2,3,4,5,6,7]
Tips :
2 <= n <= 1001 <= m <= 100rounds.length == m + 11 <= rounds[i] <= nrounds[i] != rounds[i + 1], among0 <= i < m
Two 、 Method 1
simulation
class Solution {
public List<Integer> mostVisited(int n, int[] rounds) {
List<Integer> res = new ArrayList<>();
int start = rounds[0];
int end = rounds[rounds.length - 1];
if (start <= end) {
for (int i = start; i <= end; i++) {
res.add(i);
}
} else {
for (int i = 1; i <= end; i++) {
res.add(i);
}
for (int i = start; i <= n; i++) {
res.add(i);
}
}
return res;
}
}
Complexity analysis
Time complexity :O(N). Maximum distance between start point and end point N-1 Sectors .
Spatial complexity :O(1). In addition to the answer array , We only need constant space to store several variables .
边栏推荐
- Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
- MP4格式详解
- Chapter 2 shell operation of hfds
- Study notes of Tutu - process
- Chapter 5 detailed explanation of consumer groups
- Spark's RDD (elastic distributed data set) returns a large result set
- Spark独立集群动态上线下线Worker节点
- 业务系统兼容数据库Oracle/PostgreSQL(openGauss)/MySQL的琐事
- 第6章 DataNode
- Codeforces Global Round 19
猜你喜欢

Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)

【锟斤拷】的故事:谈谈汉字编码和常用字符集

js封装数组反转的方法--冯浩的博客

Li Kou - 298th weekly match

Codeforces round 797 (Div. 3) no f

Click QT button to switch qlineedit focus (including code)

简单尝试DeepFaceLab(DeepFake)的新AMP模型

QT simulates mouse events and realizes clicking, double clicking, moving and dragging

第三章 MapReduce框架原理

Solve the single thread scheduling problem of intel12 generation core CPU (II)
随机推荐
解决Intel12代酷睿CPU单线程只给小核运行的问题
Advancedinstaller installation package custom action open file
Codeforces Round #798 (Div. 2)A~D
Mp4 format details
Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
生成随机密码/验证码
Chapter 2 shell operation of hfds
简单尝试DeepFaceLab(DeepFake)的新AMP模型
Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction
顺丰科技智慧物流校园技术挑战赛(无t4)
SF smart logistics Campus Technology Challenge (no T4)
Market trend report, technological innovation and market forecast of desktop electric tools in China
sublime text 代码格式化操作
Input can only input numbers, limited input
SQL快速入门
<li>圆点样式 list-style-type
ByteDance new programmer's growth secret: those glittering treasures mentors
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
Calculate the time difference
Problem - 1646C. Factorials and Powers of Two - Codeforces