Given an integer n
and an integer array rounds
. We have a circular track which consists of n
sectors labeled from 1
to n
. A marathon will be held on this track, the marathon consists of m
rounds. The ith
round starts at sector rounds[i - 1]
and ends at sector rounds[i]
. For example, round 1 starts at sector rounds[0]
and ends at sector rounds[1]
Return an array of the most visited sectors sorted in ascending order.
Notice that you circulate the track in ascending order of sector numbers in the counter-clockwise direction (See the first example).
Example 1:
Input: n = 4, rounds = [1,3,1,2] Output: [1,2] Explanation: The marathon starts at sector 1. The order of the visited sectors is as follows: 1 --> 2 --> 3 (end of round 1) --> 4 --> 1 (end of round 2) --> 2 (end of round 3 and the marathon) We can see that both sectors 1 and 2 are visited twice and they are the most visited sectors. Sectors 3 and 4 are visited 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]
Constraints:
2 <= n <= 100
1 <= m <= 100
rounds.length == m + 1
1 <= rounds[i] <= n
rounds[i] != rounds[i + 1]
for 0 <= i < m
This problem involves finding the sectors that are visited most frequently during a marathon on a circular track. The track has n
sectors (1 to n
), and the marathon consists of m
rounds. Each round starts and ends at specific sectors given in the rounds
array.
The key observation is that the sectors visited are continuous segments. Let's analyze the possible scenarios:
Scenario 1: The marathon ends at a sector greater than or equal to the starting sector (rounds[0] <= rounds[-1]
). In this case, the most visited sectors are simply the ones between the starting sector (rounds[0]
) and the ending sector (rounds[-1]
), inclusive.
Scenario 2: The marathon ends at a sector smaller than the starting sector (rounds[0] > rounds[-1]
). This signifies that the marathon went around the track at least once. The most visited sectors will be from 1 to the end sector (rounds[-1]
), and from the start sector (rounds[0]
) to n
.
We can directly implement the above logic to efficiently solve the problem. The code iterates through the sectors based on the condition whether the last round's end sector is greater or less than the first round's start sector. We then construct the list of most visited sectors accordingly.
Time Complexity: O(n), where n is the number of sectors. This is because in the worst case (scenario 2), we might iterate through all sectors. The construction of the result list also takes linear time.
Space Complexity: O(n) in the worst case. This is the space required to store the result list, which can contain all the sectors (from 1 to n). However, if we ignore the space used for the output, the space complexity would be O(1) as we use a constant amount of extra space.
The code implementations below directly reflect the explained approach:
Python3:
def mostVisited(n: int, rounds: list[int]) -> list[int]:
if rounds[0] <= rounds[-1]:
return list(range(rounds[0], rounds[-1] + 1))
else:
return list(range(1, rounds[-1] + 1)) + list(range(rounds[0], n + 1))
Java:
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Integer> mostVisited(int n, int[] rounds) {
List<Integer> result = new ArrayList<>();
if (rounds[0] <= rounds[rounds.length - 1]) {
for (int i = rounds[0]; i <= rounds[rounds.length - 1]; i++) {
result.add(i);
}
} else {
for (int i = 1; i <= rounds[rounds.length - 1]; i++) {
result.add(i);
}
for (int i = rounds[0]; i <= n; i++) {
result.add(i);
}
}
return result;
}
}
C++:
#include <vector>
class Solution {
public:
std::vector<int> mostVisited(int n, std::vector<int>& rounds) {
std::vector<int> result;
if (rounds[0] <= rounds.back()) {
for (int i = rounds[0]; i <= rounds.back(); ++i) {
result.push_back(i);
}
} else {
for (int i = 1; i <= rounds.back(); ++i) {
result.push_back(i);
}
for (int i = rounds[0]; i <= n; ++i) {
result.push_back(i);
}
}
return result;
}
};
JavaScript:
/**
* @param {number} n
* @param {number[]} rounds
* @return {number[]}
*/
var mostVisited = function(n, rounds) {
let result = [];
if (rounds[0] <= rounds[rounds.length - 1]) {
for (let i = rounds[0]; i <= rounds[rounds.length - 1]; i++) {
result.push(i);
}
} else {
for (let i = 1; i <= rounds[rounds.length - 1]; i++) {
result.push(i);
}
for (let i = rounds[0]; i <= n; i++) {
result.push(i);
}
}
return result;
};
These code snippets showcase the solution's efficiency and readability across different programming languages. They all follow the same core logic, adapting only to the syntactic differences between languages.