BFS with optimizations
Code\n\nfrom sortedcontainers import SortedList\nclass Solution:\n    def minReverseOperations(self, n: int, p: int, banned: List[int], k: int) -> List[int]:\n 
# Code\n```\nfrom sortedcontainers import SortedList\nclass Solution:\n    def minReverseOperations(self, n: int, p: int, banned: List[int], k: int) -> List[int]:\n        banned = set(banned)\n        ans = [-1] * n\n        for i in banned:\n            ans[i] = -2\n        \n        un = [SortedList(), SortedList()]\n        \n        for i in range(n):\n            if ans[i] != -2:\n                un[i % 2].add(i)\n        \n        def getNei(i):\n            nei = []\n            # shrink the available range so that we dont need to loop so many rounds\n            left = max(i - k + 1, k - i - 1)\n            right = min(i+k, n - (i - n + k) + 1)\n            \n            # take the index from the correct list only, reduce half the time\n            if k % 2 == i % 2:\n                arr = un[1]\n            else:\n                arr = un[0]\n            \n            idx = arr.bisect_left(left)\n            N = len(arr)\n            for x in range(idx, N):\n                j = arr[x]\n                if j >= right: # out of range\n                    break\n                nei.append(j)\n            \n            return nei\n                    \n        ans[p] = 0\n        q = deque([p])\n        un[p % 2].remove(p)\n\n        # bfs with unvisited neighbors\n        while q:\n            node = q.popleft()\n            dist = ans[node]\n            for nei in getNei(node):\n                q.append(nei)\n                ans[nei] = dist+1\n                un[nei % 2].remove(nei)\n                    \n        for i in banned:\n            ans[i] = -1\n        return ans\n```
['Python3']