当前位置:网站首页>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 .
边栏推荐
- sublime text 代码格式化操作
- (lightoj - 1323) billiard balls (thinking)
- 原生js实现全选和反选的功能 --冯浩的博客
- Market trend report, technical innovation and market forecast of double-sided foam tape in China
- 第5章 NameNode和SecondaryNameNode
- 音视频开发面试题
- Study notes of Tutu - process
- 视频压缩编码和音频压缩编码基本原理
- CMake Error: Could not create named generator Visual Studio 16 2019解决方法
- Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
猜你喜欢

Kubernetes cluster deployment

Gridhome, a static site generator that novices must know

Simple records of business system migration from Oracle to opengauss database

Cmake Express

Summary of game theory

业务系统从Oracle迁移到openGauss数据库的简单记录

Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog

Base dice (dynamic programming + matrix fast power)

Raspberry pie 4B installation opencv3.4.0

Local visualization tools are connected to redis of Alibaba cloud CentOS server
随机推荐
Useeffect, triggered when function components are mounted and unloaded
Base dice (dynamic programming + matrix fast power)
Codeforces Round #798 (Div. 2)A~D
Study notes of Tutu - process
Gridhome, a static site generator that novices must know
(lightoj - 1369) answering queries (thinking)
Codeforces Global Round 19
Specify the format time, and fill in zero before the month and days
Kubernetes集群部署
Codeforces Round #803 (Div. 2)A~C
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
MariaDB的安装与配置
Chapter 5 detailed explanation of consumer groups
antd upload beforeUpload中禁止触发onchange
Research Report on market supply and demand and strategy of China's tetraacetylethylenediamine (TAED) industry
第7章 __consumer_offsets topic
Research Report on market supply and demand and strategy of Chinese table lamp industry
力扣leetcode第 280 场周赛
提交Spark应用的若干问题记录(sparklauncher with cluster deploy mode)
Chapter 5 namenode and secondarynamenode