Most Visited Sector in a Circular Track — LeetCode 1560 Python Solution

EasyArraySimulation
Problem
#1560
Reading time
2 min

The problem

Given an integer n and an integer array rounds. We have a circular track which consists of n sectors labeled from 1 to n.

Example

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:

Python solution

Python
class Solution:
    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:
        if rounds[0] <= rounds[-1]:
            return list(range(rounds[0], rounds[-1] + 1))
        return list(range(1, rounds[-1] + 1)) + list(range(rounds[0], n + 1))

Complexity

MeasureComplexity
TimeO(n), where n is the number of sectors
SpaceO(1) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 1560. Most Visited Sector in a Circular Track?
LeetCode 1560. Most Visited Sector in a Circular Track is rated Easy on LeetCode.
What is the time complexity of LeetCode 1560. Most Visited Sector in a Circular Track?
The Python solution on this page runs in O(n), where n is the number of sectors.
What is the space complexity of LeetCode 1560. Most Visited Sector in a Circular Track?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1560. Most Visited Sector in a Circular Track cover?
LeetCode 1560. Most Visited Sector in a Circular Track is tagged Array and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview