Domain 1EasyCORE

Most Visited Sector in a Circular Track

The problem involves determining the most visited sectors on a circular track during a marathon. The track has `n` sectors labeled from 1 to `n`. The marathon consists of multiple rounds, where each round starts at one sector and ends at another. We need to return an array of the most visited sectors in ascending order.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Array

1560. Most Visited Sector in a Circular Track

Model: qwen2.5-coder:14b
Topics: Array
FAANG asked: 0
Acceptance rate: 56.8%


Problem Breakdown

The problem involves determining the most visited sectors on a circular track during a marathon. The track has n sectors labeled from 1 to n. The marathon consists of multiple rounds, where each round starts at one sector and ends at another. We need to return an array of the most visited sectors in ascending order.

Inputs:

  • An integer n representing the number of sectors on the track.
  • An integer array rounds where rounds[i] represents the end sector of the i-th round, and rounds[0] is the start sector of the first round.

Outputs:

  • An array of integers representing the most visited sectors in ascending order.

Implicit Constraints or Tricky Edge Cases:

  • The track is circular, meaning after sector n, the next sector is 1.
  • The number of rounds can be large, so an efficient solution is necessary.
  • The start and end sectors of each round are distinct.

Intuition

A brute-force approach would involve simulating the entire marathon by iterating through each round and keeping track of the visit count for each sector. This approach would have a time complexity of O(m * n), where m is the number of rounds, which is inefficient given the constraints.

The key insight is that we only need to consider the start and end sectors of the entire marathon. The most visited sectors will be those between the first sector (rounds[0]) and the last sector (rounds[-1]). If rounds[0] is less than or equal to rounds[-1], the most visited sectors are simply the range from rounds[0] to rounds[-1]. If rounds[0] is greater than rounds[-1], it means we have wrapped around the track, and the most visited sectors are from 1 to rounds[-1] and from rounds[0] to n.

Approach

  1. Identify the start sector (start = rounds[0]) and the end sector (end = rounds[-1]).
  2. If start is less than or equal to end, return the range from start to end.
  3. If start is greater than end, return the concatenation of ranges from 1 to end and from start to n.

Complexity Analysis

  • Time Complexity: O(1) for determining the most visited sectors, as we only need a constant amount of computation.
  • Space Complexity: O(n) in the worst case when all sectors are included in the result.

Solution

def mostVisited(n: int, rounds: List[int]) -> List[int]:
    start = rounds[0]
    end = rounds[-1]
    
    if start <= end:
        return list(range(start, end + 1))
    else:
        return list(range(1, end + 1)) + list(range(start, n + 1))

Edge Cases Handled

  • Single Sector Track (n = 1): The code will correctly handle this by returning [1].
  • All Sectors Visited: If start is 1 and end is n, the code returns all sectors from 1 to n.
  • Wrapping Around the Track: If start is greater than end, the code correctly handles the wrap-around by including sectors from 1 to end and from start to n.