My O(n) two-deque solution exceeds time limit
It TLE\'s at 822/825 :(, any tips would be greatly appreciated\n\nclass Solution {\npublic:\n  long long minimumMoves(vector<int>& nums, int k, int maxChanges) 
It TLE\'s at 822/825 :(, any tips would be greatly appreciated\n```\nclass Solution {\npublic:\n  long long minimumMoves(vector<int>& nums, int k, int maxChanges) {\n    int n = nums.size();\n    vector<int> oneIdcs;\n    for (int i = 0; i < n; ++i) {\n      if (nums[i] == 1) oneIdcs.push_back(i);\n    }\n    if (oneIdcs.empty()) return k * 2;\n    long long minMoves = -1;\n    int m = oneIdcs.size();\n    deque<int> left, right;\n    long long leftSum = 0, rightSum = 0;\n    //left.push_back(0);\n    long long diff = 0;\n    for (int i = 0; i < m; ++i) {\n      leftSum += (left.size() * diff);\n      rightSum -= ((right.size()) * diff);\n      if (!right.empty() && right.front() == i) right.pop_front();\n      long long changes = 0;\n      while (left.size() + right.size() + 1 > k) {\n        leftSum -= (oneIdcs[i] - oneIdcs[left.front()]);\n        left.pop_front();\n      }\n      int tot = left.size() + right.size() + 1;\n      int rightMost = right.empty() ? i : right.back();\n      while (tot < k) {\n        if (rightMost + 1 < m &&\n            oneIdcs[rightMost + 1] - oneIdcs[i] < 2) {\n          right.push_back(rightMost + 1);\n          rightSum += (oneIdcs[rightMost + 1] - oneIdcs[i]);\n          rightMost++;\n          tot++;\n        } else if (changes < maxChanges) {\n          changes++;\n          tot++;\n        } else if (rightMost + 1 < m) {\n          right.push_back(rightMost + 1);\n          rightSum += (oneIdcs[rightMost + 1] - oneIdcs[i]);\n          rightMost++;\n          tot++;\n        } else {\n          break;\n        }\n      }\n      while (tot == k && !left.empty() && changes < maxChanges) {\n        if (oneIdcs[i] - oneIdcs[left.front()] > 2) {\n          leftSum -= (oneIdcs[i] - oneIdcs[left.front()]);\n          left.pop_front();\n          changes++;\n        } else {\n          break;\n        }\n      }\n      while ((tot == k) && (rightMost + 1 < m) && !left.empty() &&\n            (oneIdcs[rightMost + 1] - oneIdcs[i]) <\n             (oneIdcs[i] - oneIdcs[left.front()])) {\n        leftSum -= (oneIdcs[i] - oneIdcs[left.front()]);\n        left.pop_front();\n        rightSum += (oneIdcs[rightMost + 1] - oneIdcs[i]);\n        right.push_back(rightMost + 1);\n        rightMost++;\n      }\n      \n      long long moves = leftSum + rightSum + changes * 2;\n      if (tot == k) {\n        minMoves = (minMoves == -1) ? moves : min(minMoves, moves);\n      }\n      if (i + 1 < m) diff = oneIdcs[i + 1] - oneIdcs[i];\n      left.push_back(i);\n    }\n    return minMoves;\n  }\n};\n```
[]