当前位置:网站首页>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 <= 100
1 <= m <= 100
rounds.length == m + 1
1 <= rounds[i] <= n
rounds[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 .
边栏推荐
- Hbuilder X格式化快捷键设置
- 顺丰科技智慧物流校园技术挑战赛(无t4)
- 浏览器打印边距,默认/无边距,占满1页A4
- 两个礼拜速成软考中级软件设计师经验
- Codeforces Round #803 (Div. 2)A~C
- Summary of FTP function implemented by qnetworkaccessmanager
- Codeforces Round #797 (Div. 3)无F
- Codeforces Global Round 19
- (lightoj - 1369) answering queries (thinking)
- Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction
猜你喜欢
Codeforces Round #801 (Div. 2)A~C
<li>圆点样式 list-style-type
Li Kou: the 81st biweekly match
第5章 消费者组详解
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
两个礼拜速成软考中级软件设计师经验
Raspberry pie 4B installation opencv3.4.0
FLV格式详解
Tree of life (tree DP)
Audio and video development interview questions
随机推荐
Acwing: the 56th weekly match
Li Kou: the 81st biweekly match
Basic principles of video compression coding and audio compression coding
Useeffect, triggered when function components are mounted and unloaded
JS time function Daquan detailed explanation ----- AHAO blog
Solve the single thread scheduling problem of intel12 generation core CPU (II)
JS encapsulates the method of array inversion -- Feng Hao's blog
Codeforces Round #802(Div. 2)A~D
Local visualization tools are connected to redis of Alibaba cloud CentOS server
Codeforces Round #800 (Div. 2)AC
CMake Error: Could not create named generator Visual Studio 16 2019解决方法
第6章 Rebalance详解
useEffect,函数组件挂载和卸载时触发
第7章 __consumer_offsets topic
第5章 消费者组详解
QT implementation window gradually disappears qpropertyanimation+ progress bar
Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
使用jq实现全选 反选 和全不选-冯浩的博客
Specify the format time, and fill in zero before the month and days
Educational Codeforces Round 122 (Rated for Div. 2)