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; } };
classUF { public: vector<int> f; vector<int> size; int n; UF(int _n): n(_n), f(_n), size(_n, 1) { iota(f.begin(), f.end(), 0); } intfind(int x){ return f[x] == x ? x : f[x] = find(f[x]); } void _union(int x, int y) { x = find(x); y = find(y); if (x != y) { if (size[x] < size[y]) swap(x, y); f[y] = x; size[x] += size[y]; } } }; classSolution { public: intminSwapsCouples(vector<int>& row){ constint n = row.size(); int tot = n / 2; UF uf(tot); for (int i = 0; i < n; i += 2) uf._union(row[i] / 2, row[i + 1] / 2); unordered_map<int, int> m; for (int i = 0; i < tot; ++i) ++m[uf.find(i)]; int ans = 0; // for each connected set with "sz" as size, // "sz - 1" would be the number of needed swaps. for (constauto& [_, sz] : m) ans += sz - 1; return ans; } };
classSolution { public: intminSwapsCouples(vector<int>& row){ constint n = row.size(); int tot = n / 2; vector<vector<int>> graph(tot); for (int i = 0; i < n; i += 2) { int l = row[i] / 2; int r = row[i + 1] / 2; graph[l].emplace_back(r); graph[r].emplace_back(l); } vector<bool> visited(tot, false); int ans = 0; for (int i = 0; i < tot; ++i) if (!visited[i]) { queue<int> q; q.push(i); visited[i] = true; int cnt = 0; while (!q.empty()) { int x = q.front(); q.pop(); ++cnt; for (int nx : graph[x]) if (!visited[nx]) { q.push(nx); visited[nx] = true; } } ans += cnt - 1; } return ans; } };