by myself
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```\nimport java.util.*;\n\nclass Solution {\n\n    // Idea 2 from Mr Guan: extract all indexes of 1s into List and do sliding window on that\n    // every time we shift the window, we can update the cost of the window in constant time\n\n    // Find best subArr with K 1s and merge it at median!!!\n\n    //  ---->   <----\n    // [1,0,0,1,0,0,1], k = 3   res = 4\n\n\n    //  ---------->\n    // [1,0,0,0,0,0,1,1], k = 3\n\n    // [1,0,0,1,0,1,1,0,0,1,0,1]    k = 4\n    //\n    // [1,0,0,1,0,1,1] = 4\n    //       [1,0,1,1,0,0,1] = 3\n    //           [1,1,0,0,1,0,1] = 5\n\n    // cost to append all 1s on left to index i:\n    // leftCost[i] = sum(numOnesLeftOfEachZero[0] + ... numOnesLeftOfEachZero[i - 1])\n\n    public int minMoves(int[] A, int k) {\n        if (k == 1) return 0;\n        int n = A.length;\n        int best = Integer.MAX_VALUE;\n\n        int[] rightCost = new int[n];\n        int numRightOnes = A[n - 1];\n        for (int i = n - 2; i >= 0; i--) {\n            rightCost[i] = rightCost[i + 1];\n            if (A[i] == 0) rightCost[i] += numRightOnes;\n            numRightOnes += A[i];\n        }\n\n        // media id = (k / 2) + (k % 2)\n        Deque<Integer> medians = new ArrayDeque<>(k / 2 + 1);\n        // 4 elems: 1,2,3,4  => median = 3\n        // 3 elems: 1,2,3    => median = 2\n        int offerMedianTh = k / 2;\n\n\n        int[] leftCost = new int[n];\n        int r = 0;\n        int onesInside = 0;\n        int numLeftOnes = 0;\n        int numLeftOutside = 0;\n        for (int l = 0; l < n; l++, onesInside--, numLeftOutside++) { // r = first outside\n            while (r < n && onesInside < k) {\n                if (A[r] == 1) {\n                    onesInside++;\n                    if (onesInside > offerMedianTh) medians.offer(r);\n                }\n\n                if (r > 0) leftCost[r] = leftCost[r - 1];\n                if (A[r] == 0) leftCost[r] += numLeftOnes;\n                numLeftOnes += A[r];\n                r++;\n            }\n            if (onesInside < k) break;\n            while (A[l] == 0) l++; // cut down left garbage\n\n            int end = r - 1;\n            int start = l;\n            int medianId = medians.poll();\n\n            int startToMed = leftCost[medianId] - leftCost[start];\n            int endToMed = rightCost[medianId] - rightCost[end];\n\n\n            int leftInside = k / 2;\n            int rightInside = k - 1 - leftInside;\n\n            // to subtract\n            int leftMaxDist = medianId - start - leftInside;\n            int rightMaxDist = end - medianId - rightInside;\n\n            int numRightOutside = numRightOnes - numLeftOutside - onesInside; // total - leftOutside - inside\n\n            int leftOutside = numLeftOutside * leftMaxDist;\n            int rightOutside = numRightOutside * rightMaxDist;\n            /////////////\n\n            int left = startToMed - leftOutside;\n            int right = endToMed - rightOutside;\n            int currCost = left + right;\n            best = Math.min(best, currCost);\n        }\n\n\n        return best;\n    }\n    // 1,0,0,1,1,1,1,0,1,1,0,0,1,1\n    // 5 + 4 * 3 + 2*2 = 5 + 12 + 4 = 21\n\n    // 1,0,0,0,0,0,1,1,0,0,1,0,0,1,0,0,0,1,1,1,1\n    // 1,0,0,0,0,0,1                            = 5\n    // 1,0,0,0,0,0,1,1                          = 5\n    // 1,0,0,0,0,0,1,1,0,0,1                    = 7\n    // 1,0,0,0,0,0,1,1,0,0,1,0,0,1              = 9\n    // 1,0,0,0,0,0,1,1,0,0,1,0,0,1,0,0,0,1      = 12\n    // 1,0,0,0,0,0,1,1,0,0,1,0,0,1,0,0,0,1,1    = 12\n    // 1,0,0,0,0,0,1,1,0,0,1,0,0,1,0,0,0,1,1,1  = 12\n    // 1,0,0,0,0,0,1,1,0,0,1,0,0,1,0,0,0,1,1,1,1 = 12\n    //  10 + 7 + 9 + 4 * 12 = 74\n    // 8 + 8 + 8\n\n    // [185, 169, 153, 137, 121, 121, 106, 91, 91, 91, 91, 91, 80, 80, 80, 71, 62, 62, 62, 55, 48, 41, 34, 27, 27, 27, 22, 17, 17, 13, 9, 9, 6, 3, 0, 0, 0, 0]\n    // [0,0,0,0,1,0,0,1,1,1,1,0,1,1,0,0,1,1,0,0,0,0,0,1,1,0,0,1,0,0,1,0,0,0,1,1,1,1]\n    //          4                         m                                       37  38\n    //          l = start                                                         end  r\n\n\n    // 38\n    // 4 37 med 17\n    // startToMed 21\n    // endToMed 62\n    // leftCost  [0, 0, 0, 0, 0, 1, 2, 2, 2, 2, 2, 7, 7, 7, 14, 21, 21, 21, 30, 39, 48, 57, 66, 66, 66, 77, 88, 88, 100, 112, 112, 125, 138, 151, 151, 151, 151, 151]\n    // rightCost\n\n\n}\n\n```
['Java']