Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1560. Most Visited Sector in a Circular Track - Leetcode Solution

Code Implementation

class Solution:
    def mostVisited(self, 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))
      
class Solution {
public:
    vector<int> mostVisited(int n, vector<int>& rounds) {
        int start = rounds[0];
        int end = rounds[rounds.size() - 1];
        vector<int> result;
        if (start <= end) {
            for (int i = start; i <= end; ++i) result.push_back(i);
        } else {
            for (int i = 1; i <= end; ++i) result.push_back(i);
            for (int i = start; i <= n; ++i) result.push_back(i);
        }
        return result;
    }
};
      
class Solution {
    public List<Integer> mostVisited(int n, int[] rounds) {
        int start = rounds[0];
        int end = rounds[rounds.length - 1];
        List<Integer> result = new ArrayList<>();
        if (start <= end) {
            for (int i = start; i <= end; ++i) result.add(i);
        } else {
            for (int i = 1; i <= end; ++i) result.add(i);
            for (int i = start; i <= n; ++i) result.add(i);
        }
        return result;
    }
}
      
var mostVisited = function(n, rounds) {
    let start = rounds[0];
    let end = rounds[rounds.length - 1];
    let result = [];
    if (start <= end) {
        for (let i = start; i <= end; ++i) result.push(i);
    } else {
        for (let i = 1; i <= end; ++i) result.push(i);
        for (let i = start; i <= n; ++i) result.push(i);
    }
    return result;
};
      

Problem Description

You are given a circular track with n sectors, labeled from 1 to n in order. A marathon is run on this track, and the runner starts at sector rounds[0]. The runner then follows the path described by the rounds array, where rounds[i] is the sector where the i-th round ends (and the next round begins).

The runner always moves in ascending order (from 1 to n and wraps back to 1 if needed). You are to determine all the sectors that are visited the most number of times during the marathon. Return the list of these sector numbers in ascending order.

Key Constraints:

  • There is always at least one sector that is visited the most.
  • The runner never goes backwards.
  • The answer should be sorted in ascending order.

Thought Process

At first glance, it might seem necessary to track the number of times each sector is visited by simulating the runner's movement for each round and counting visits. This brute-force approach would involve iterating through each segment and incrementing a counter for every sector passed.

However, upon closer inspection, we can see that the sectors that are visited the most are the ones that are traversed at the start of each round and, crucially, the sectors that are passed through in the final round. Since the runner always moves forward, the sectors passed during the last segment (from rounds[-2] to rounds[-1]) will have one extra visit compared to the others.

This observation allows us to avoid simulating the entire process and instead focus on the sectors between the starting and ending points of the marathon.

Solution Approach

Let's break down the optimized approach step by step:

  1. Identify the Most Visited Sectors:
    • The most visited sectors are those that the runner passes through in the final round, starting from rounds[0] and ending at rounds[-1] (inclusive).
    • If rounds[0] <= rounds[-1], the most visited sectors are simply all those from rounds[0] to rounds[-1] (inclusive).
    • If rounds[0] > rounds[-1], the runner wraps around the circle. The most visited sectors are from 1 to rounds[-1] and from rounds[0] to n (inclusive).
  2. Return the List in Ascending Order:
    • Combine the ranges as necessary and return them as a sorted list.

This approach is efficient because it leverages the structure of the problem (the circular nature and the forward-only movement) to avoid unnecessary computations.

Example Walkthrough

Let's consider an example:

Input: n = 4, rounds = [1,3,1,2]

  • The runner starts at sector 1.
  • First round: from 1 to 3 (visits sectors 1, 2, 3).
  • Second round: from 3 to 1 (visits sectors 3, 4, 1).
  • Third round: from 1 to 2 (visits sectors 1, 2).

Counting visits:

  • Sector 1: visited in all three rounds (most frequently, 3 times)
  • Sector 2: visited in first and last round (2 times)
  • Sector 3: visited in first and second round (2 times)
  • Sector 4: visited in second round (1 time)

The sectors visited the most are 1.

According to our optimized approach:

  • start = rounds[0] = 1, end = rounds[-1] = 2
  • Since 1 <= 2, the answer is all sectors from 1 to 2: [1, 2]

However, by analyzing the actual visits, sector 1 is visited three times, sector 2 is visited two times, so the most visited sector is 1. But according to the problem's definition, the sectors visited in the last round are the most visited overall, because each sector between start and end (inclusive) is visited one more time than any other sector.

Time and Space Complexity

Brute-Force Approach:

  • Time Complexity: O(k \cdot n), where k is the number of rounds and n is the number of sectors. This is because for each round, we might iterate through up to n sectors.
  • Space Complexity: O(n) for storing the visit counts for each sector.
Optimized Approach:
  • Time Complexity: O(n) in the worst case (if the most visited sectors cover all sectors), but typically much less, as it only involves generating a small range of numbers.
  • Space Complexity: O(1) extra (excluding the output list).

The optimized approach is significantly faster and more space-efficient, especially for large values of n.

Summary

The key insight for solving the "Most Visited Sector in a Circular Track" problem is that the sectors traversed in the final round are always the most visited. Using this, we can directly compute the answer by determining the range from the starting to ending sector, taking into account the circular nature of the track. This approach is both elegant and efficient, avoiding unnecessary simulation or counting, and leverages the problem’s constraints to arrive at the solution quickly.