Python - Always start from block 1, eliminate all previous rounds.
Assume that we always start from block 1, the problem is not that hard:\n\n1. Detect cur value is smaller than prev, if True, a circle is made, we can eliminate
Assume that we **always start from block 1**, the problem is not that hard:\n\n1. Detect `cur` value is smaller than `prev`, if True, a circle is made, we can eliminate it.\n\n\tFor example, `1 -> 3 -> 5 -> 2` is eqals to `1 -> 2`\n\n2. We can record the round window using a stack, if a round is made, reset the stack to `[1]`\n3. Traversal the blocks, our stack will end up without any cycle/round, e.g. `[1]`, `[1, 3]`, `[1, 3, 5]` \n\nWe only care about the start and end value, which are `stack[0]`, `stack[-1]`, represent a single track. Now you get the idea.\n\n**What if we start from none-1?**\n\nIf so, we would like to mock the block ID, for example `[4,3,2,1]` will be transfered into `[1,4,3,2]`, it\'s actually a rotate problem: [189. Rotate Array](https://leetcode.com/problems/rotate-array/), or we can write some function to trasfer it.\n\nIf we want to rebase 4 to 1, we need to move backward 4-1=3 steps (call it `adj`). When we need to transfer it back, we need to move forward 3 steps.\n\nIf `i - adj < 0`, we can borrow the `n`.\nIf `i + adj > n`, use the `modular` operation (its an operator work on circle). \n\nThen we can come up with two transfer function like these:\n\n* `rebase(4)` = 1\n* `revert(1)` = 4\n\n**Rebase -> Revert -> Dealing the output**\n\nAt the very begining, we come up with `start`, `end`, which start is always `1` since the block are all **rebased**. Now we **revert** the block value. \n\nThere are 3 base cases for our final track:\n\n1. `start == end`: the result is `[start]`\n2. `start < end`: the result is `range([start: end+1])`\n3. `start > end`: the result is `[start, start+1, start+2, ... n, 1, 2, 3, ... end]`, for example, start=3 and end=2, the result is `[3, 4, 1, 2]`\n\n\n```\nclass Solution:\n    def mostVisited(self, n: int, rounds: List[int]) -> List[int]:\n        \n        # how many step to backtrack to make the "start point" arrive 1\n        adj = rounds[0] - 1\n        \n        def rebase(i):\n            return i - adj if i > adj else i + n - adj\n        \n        def revert(i):\n            i += adj\n            return i if i <= n else i % n\n        \n        stack = []\n        \n        # apply the rebase function, make sure we always start from 1\n        for cur in map(rebase, rounds):\n            \n            if not stack:\n                stack.append(cur)\n                continue\n                \n            prev = stack[-1]\n            \n            if cur < prev:\n                # a circle is been made, reset history\n                stack = [1]\n                \n            if cur > 1:\n                stack.append(cur)\n\n        \n        # extrack start and end block, revert back to the origin value\n        \n        start, end = map(revert, (stack[0], stack[-1]))\n        \n        if start == end:\n            # case like [2]\n            \n            return [start]\n        \n        elif start > end:\n            # case: [3, 1]\n            \n            return list(range(1, end+1)) + list(range(start, n+1))\n            \n        else:\n            # case: [1, 3]\n            return list(range(start, end+1))\n```
['Python']