leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0995.cpp (482B)


      1 class Solution {
      2   public:
      3     int minKBitFlips(vector<int> &nums, int k) const {
      4         const int n = size(nums);
      5         int res = 0, flips = 0;
      6 
      7         for (int i = 0; i < n; i++) {
      8             if (nums[i] == flips % 2) {
      9                 if (i > n - k) return -1;
     10                 nums[i] -= 2, res++, flips++;
     11             }
     12 
     13             const int prev = i - k + 1;
     14             if (i >= k - 1 && nums[prev] < 0) nums[prev] += 2, flips--;
     15         }
     16 
     17         return res;
     18     }
     19 };