classSolution { public: intminKBitFlips(vector<int>& A, int K){ int n = A.size(), flipped = 0, ans = 0; vector<int> isFlipped(n); for (int i = 0; i < n; ++i) { if (i >= K) flipped ^= isFlipped[i - K]; if (flipped == A[i]) { if (i + K > n) return-1; isFlipped[i] = 1; flipped ^= 1; ++ans; } } return ans; } };
classSolution { public: intminKBitFlips(vector<int>& A, int K){ int n = A.size(), flipped = 0, ans = 0; queue<int> isFlipped; for (int i = 0; i < n; ++i) { if (i >= K) { flipped ^= isFlipped.front(); isFlipped.pop(); } if (flipped == A[i]) { if (i + K > n) return-1; isFlipped.push(1); flipped ^= 1; ++ans; } else { isFlipped.push(0); } } return ans; } };
classSolution { public: intminKBitFlips(vector<int>& A, int K){ // "cur" is the count of flipping times within current sliding window. int n = A.size(), cur = 0, ans = 0; for (int i = 0; i < n; ++i) { if (i >= K && A[i - K] > 1) { --cur; A[i - K] += 2; } if (cur % 2 == A[i]) { if (i + K > n) return-1; A[i] += 2; ++cur; ++ans; } } return ans; } };
classSolution { public: intarrayPairSum(vector<int>& nums){ sort(nums.begin(), nums.end()); int n = nums.size(), ans = 0; for (int i = 0; i < n; i += 2) ans += nums[i]; return ans; } };
classSolution { public: intlongestSubstring(string s, int k){ int n = s.size(), ans = 0; for (int cnt = 1; cnt <= 26; ++cnt) { // there could be [1..26] diff letters. int left = 0, right = 0, uniqueCnt = 0; int charCnt[26] = {0}; while (right < n) { bool valid = true; if (charCnt[s[right++] - 'a']++ == 0) ++uniqueCnt; while (uniqueCnt > cnt) if (--charCnt[s[left++] - 'a'] == 0) --uniqueCnt; for (int j = 0; j < 26; ++j) if (charCnt[j] > 0 && charCnt[j] < k) { valid = false; break; } if (valid) ans = max(ans, right - left); } } return ans; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intlongestSubstring(string s, int k){ int n = s.size(), m[26] = {0}, left = 0, ans = 0; bool valid = true; for (char c : s) ++m[c - 'a']; for (int right = 0; right < n; ++right) if (m[s[right] - 'a'] < k) { ans = max(ans, longestSubstring(s.substr(left, right - left), k)); valid = false; left = right + 1; } return valid ? n : max(ans, longestSubstring(s.substr(left, n - left), k)); } };
classSolution { public: intlongestOnes(vector<int>& A, int K){ int n = A.size(), left = 0, right = 0, ans = 0; vector<int> P(n + 1); for (int i = 1; i <= n; ++i) P[i] = P[i - 1] + (1 - A[i - 1]); while (right++ < n) { left = lower_bound(P.begin(), P.end(), P[right] - K) - P.begin(); ans = max(ans, right - left); } return ans; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: intlongestOnes(vector<int>& A, int K){ int n = A.size(), left = 0, right = 0, cnt = 0, ans = 0; while (right < n) { if (A[right++] == 0) ++cnt; while (cnt > K) if (A[left++] == 0) --cnt; ans = max(ans, right - left); } return ans; } };
classSolution { public: intfindMaxConsecutiveOnes(vector<int>& nums){ int cur = 0, cnt = 0, ans = 0; for (int num : nums) { ++cnt; if (num == 0) { cur = cnt; cnt = 0; } ans = max(ans, cur + cnt); } return ans; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: intfindMaxConsecutiveOnes(vector<int>& nums){ int n = nums.size(), left = 0, right = 0, cnt = 0, ans = 0; while (right < n) { if (nums[right++] == 0) ++cnt; while (cnt > 1) if (nums[left++] == 0) --cnt; ans = max(ans, right - left); } return ans; } };
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// solution for follow up (nums as streaming data). classSolution { public: intfindMaxConsecutiveOnes(vector<int>& nums){ int n = nums.size(), left = 0, right = 0, ans = 0; queue<int> q; while (right < n) { if (nums[right++] == 0) q.push(right); if (q.size() > 1) { left = q.front(); q.pop(); } ans = max(ans, right - left); } return ans; } };
classSolution { public: intfirstMissingPositive(vector<int>& nums){ constint n = nums.size(); for (int i = 0; i < n; ++i) if (nums[i] <= 0) nums[i] = n + 1; // makes all nums as positive integer. for (int i = 0; i < n; ++i) { int num = abs(nums[i]); if (num <= n) nums[num - 1] = -abs(nums[num - 1]); // taged as negative integer. } for (int i = 0; i < n; ++i) if (nums[i] > 0) // have not been taged, indicating the num missed. return i + 1; return n + 1; // [1..n] all taged, the ans should be "n + 1". } };
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intfirstMissingPositive(vector<int>& nums){ constint n = nums.size(); for (int i = 0; i < n; ++i) while (nums[i] > 0 && nums[i] <= n && nums[i] != nums[nums[i] - 1]) swap(nums[i], nums[nums[i] - 1]); for (int i = 0; i < n; ++i) if (nums[i] != i + 1) return i + 1; return n + 1; } };
classSolution { public: intmissingNumber(vector<int>& nums){ int ans = nums.size(), i = 0; for (constint num : nums) ans ^= i++ ^ num; return ans; } };