WC339_4
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n\n    int[] ans;\n\n    public void calcOper(int n, int sp, int fp, int k) {\n        int finish = fp + k;\n        int curStep = ans[sp];\n        if (finish > n) {\n            for (int i = fp; i >= sp; i -= 2) {\n                if (ans[i] == curStep) {\n                    if (i <= n - k) {\n                        if (i+k>finish) finish = i + k;\n                        break;\n                    } else {\n                        finish = 2 * n - i - k;\n                        if (finish==n) break;\n                    }\n                }\n            }\n        }\n        int start = sp - k + 1;\n        if (start < 0) {\n            for (int i = sp; i <= fp; i += 2) {\n                if (ans[i] == curStep) {\n                    if (i >= k - 1) {\n                        if (i - k + 1 < start) start = i - k + 1;\n                        break;\n                    } else {\n                        start = k - 1 - i;\n                        if (start == 0) break;\n                    }\n                }\n            }\n        }\n        curStep++;\n        int nextSp = -1;\n        int nextFp = 0;\n        for (int i = start; i < finish; i += 2) {\n            if (ans[i] == -2) {\n                ans[i] = curStep;\n                if (nextSp == -1) {\n                    nextFp = nextSp = i;\n                } else nextFp = i;\n            }\n        }\n        if (nextSp != -1) calcOper(n, nextSp, nextFp, k);\n    }\n\n    public int[] minReverseOperations(int n, int p, int[] banned, int k) {\n        ans = new int[n];\n        for (int i = 0; i < n; i++) ans[i] = -2;\n        for (int i : banned) ans[i] = -1;\n        ans[p] = 0;\n        if (k == 2) {\n            for (int i = p + 1, j = 1; i < n; i++) {\n                if (ans[i] == -1) break;\n                else {\n                    ans[i] = j++;\n                }\n            }\n            for (int i = p - 1, j = 1; i >= 0; i--) {\n                if (ans[i] == -1) break;\n                else {\n                    ans[i] = j++;\n                }\n            }\n        } else if (k != 1) {\n            calcOper(n, p, p, k);\n        }\n        for (int i = 0; i < n; i++) {\n            if (ans[i] == -2) ans[i]++;\n        }\n        return ans;\n    }\n\n}\n```
['Java']