Beats 100% 🔥 JS Solution  |  O(n)
IntuitionSince the track forms a circle in ascending order of sector numbers, we only need to focus on the start and end points, which can be divided into the f
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Since the track forms a circle in ascending order of sector numbers, we only need to focus on the start and end points, which can be divided into the following three cases:

1. **`start == end`**

    - If the start and end are in the same sector, it can be seen that the start point is visited once more than other sectors.
    - Therefore, the most visited point is the start point `start`.

    ```
                s---------- n
    1 --------------------- n
    1 --------------------- n
    1 --------- e
    ```

---

2. **`start < end`**

    - If the start sector is before the end sector, it can be seen that the sector between the start and end points is visited once more than other sectors.
    - Therefore, the most visited point is `[start, end]`.

    ```
           s -------------- n
    1 --------------------- n
    1 --------------------- n
    1 --------------- e
    ```

---

3. **`start > end`**

    - If the start sector is after the end sector, it can be seen that the most visited sectors are divided into two parts, namely:
    - The sectors from the starting point `start` to the end `n` of the track (`[start, n]`);
    - The sectors from the starting position `1` of the track to the end `end` (`[1, end]`);
    - Therefore, the most visited point is `[start, n] + [1, end]`.

    ```
                    s ----- n
    1 --------------------- n
    1 --------------------- n
    1 ----- e
    ```

# Approach
<!-- Describe your approach to solving the problem. -->



1. Define variables:

    - Define variable `start = rounds[0]` to represent the starting sector;

    - Define variable `end = rounds[rounds.length - 1]` to represent the ending sector;

    - `rounds` is the order of sectors passed in the game, `rounds[0]` is the starting sector, and `rounds[rounds.length - 1]` is the ending sector.

2. Define an auxiliary function `buildArr(a, b)` to generate a continuous array of `[a, a+1, ..., b]` to simplify the code logic.

3. According to the relationship between the starting point and the end point, choose different ways to generate the result array.

    - If `start == end`, return `[start]` directly.
    - If `start < end`, return a continuous array from `start` to `end`.
    - If `start < end`, return a continuous array from `1` to `end`, plus a continuous array from `start` to `n`.

# Complexity Analysis

- **Time Complexity**: `O(n)`
    - The complexity of the auxiliary function `buildArr(a, b)` is `O(k)`, where `k` is the length of the generated continuous array, `k = b - a + 1`.
    - In the worst case, a continuous array of length `n` needs to be generated, so the time complexity is `O(n)`.
- **Space Complexity**: `O(n)`, the space occupied by the resulting array.

# Code
```javascript []
/**
 * @param {number} n
 * @param {number[]} rounds
 * @return {number[]}
 */
var mostVisited = function(n, rounds) {
    const m = rounds.length - 1
    let res = []
    const start = rounds[0]
    const end = rounds[m]

    const buildArr = (a, b) => new Array(b - a + 1).fill(a).map((a, i) => a + i)

    if (start == end) return [start]
    if (start < end) return buildArr(start, end)
    return buildArr(1, end).concat(buildArr(start, n))
};
```
['JavaScript']