leetcode

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

1004.cpp (368B)


      1 class Solution {
      2   public:
      3     int longestOnes(const vector<int> &nums, int k) {
      4         int maxi = 0, left = 0;
      5         for (int i = 0; i < nums.size(); i++) {
      6             maxi = max(maxi, i - left);
      7             if (!nums[i]) k--;
      8             while (k < 0)
      9                 if (!nums[left++]) k++;
     10         }
     11         return max(maxi, (int)nums.size() - left);
     12     }
     13 };