JS solution - two pointers + greedy
Pro tip: \u7231\u5C0F\u8C22 \uD83D\uDC9C\n\n# Complexity\n- Time complexity: O(n)\n- Space complexity: O(1)\n\n# Code\njs\n/**\n * @param {number[]} nums\n * @p
Pro tip: \u7231\u5C0F\u8C22 \uD83D\uDC9C\n\n# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n```js\n/**\n * @param {number[]} nums\n * @param {number} k\n * @param {number} maxChanges\n * @return {number}\n */\nvar minimumMoves = function (nums, k, maxChanges) {\n  let n = nums.length;\n  let res = 1e15;\n  maxChanges = Math.min(maxChanges, k);\n\n  for (let c = Math.max(0, maxChanges - 3); c <= maxChanges; ++c) {\n    let u = k - c;\n    if (u <= 0) {\n      res = Math.min(res, 2 * c);\n      continue;\n    }\n    let h = (u + 1) >> 1;\n\n    let l = 0;\n    while (l < n && !nums[l]) ++l;\n    if (l == n) {\n      continue;\n    }\n\n    let r = l;\n    let m = l;\n    let a = 0;\n\n    for (let i = 1; i < u; ++i) {\n      ++r;\n      while (r < n && !nums[r]) ++r;\n      if (r == n) {\n        break;\n      }\n    }\n    if (r == n) {\n      continue;\n    }\n\n    for (let i = 1; i < h; ++i) {\n      ++m;\n      while (!nums[m]) ++m;\n    }\n\n    for (let i = l; i <= r; ++i) {\n      if (nums[i]) {\n        a += Math.abs(i - m);\n      }\n    }\n\n    res = Math.min(res, a + 2 * c);\n\n    while (true) {\n      ++r;\n      while (r < n && !nums[r]) ++r;\n      if (r == n) {\n        break;\n      }\n\n      if (u & 1) {\n        a -= m - l;\n      }\n      ++m;\n      while (m < n && !nums[m]) ++m;\n      if (!(u & 1)) {\n        a -= m - l;\n      }\n      a += r - m;\n\n      ++l;\n      while (l < n && !nums[l]) ++l;\n\n      res = Math.min(res, a + 2 * c);\n    }\n  }\n\n  return res;\n};\n```
['JavaScript']