From 2ed15bf3cf824742014bbe1cb0551e4054e78247 Mon Sep 17 00:00:00 2001 From: aucker Date: Fri, 31 May 2024 16:27:05 +0800 Subject: [PATCH] update: Mar 29 & Mar31 [H] May31 use the optimization of Bit Ops, which reduce the time complexity from O(N^2) to O(1) --- daily/May29.cpp | 43 +++++++++++ daily/May31.cpp | 193 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 daily/May29.cpp create mode 100644 daily/May31.cpp diff --git a/daily/May29.cpp b/daily/May29.cpp new file mode 100644 index 0000000..8d17590 --- /dev/null +++ b/daily/May29.cpp @@ -0,0 +1,43 @@ +#include + +#include +using namespace std; + +class Solution { + public: + int maxmimumLength(string s) { + // at least 3 times + vector groups[26]; + int cnt = 0, n = s.length(); + for (int i = 0; i < n; i++) { + cnt++; + if (i + 1 == n || s[i] != s[i + 1]) { + groups[s[i] - 'a'].push_back(cnt); + cnt = 0; + } + } + + // unordered_map appear; + // for (char c : s) { + // appear[c]++; + // } + // bool check = false; + // for (auto it = appear.begin(); it != appear.end();) { + // if (it->second >= 3) { + // check = true; + // break; + // } + // } + int ans = 0; + + for (auto& a : groups) { + if (a.empty()) continue; + sort(a.rbegin(), a.rend()); + a.push_back(0); + a.push_back(0); // assum two empty string + ans = max({ans, a[0] - 2, min(a[0] - 1, a[1]), a[2]}); + } + + return ans ? ans : -1; + } +}; \ No newline at end of file diff --git a/daily/May31.cpp b/daily/May31.cpp new file mode 100644 index 0000000..4f4637e --- /dev/null +++ b/daily/May31.cpp @@ -0,0 +1,193 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +class Solution { + /** + * @brief use vector to store the whole value + * when n is large, this is slow, O(N^2) + * So, There does seem to be a O(1) solution. Sure! + * + * @param grid + * @return vector + */ + vector findMissingAndRepeatedValues(vector>& grid) { + int n = grid.size(); + vector appear(n * n + 1, 0); + appear[0] = 1; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + appear[grid[i][j]]++; + } + } + int a = 0, b = 0; + for (int i = 0; i < appear.size(); i++) { + if (appear[i] == 2) { + a = i; + } + if (appear[i] == 0) { + b = i; + } + } + return {a, b}; + } + + /** + * @brief use the bit ops, this is much faster + * O(1), 1 and 3 bit ops is 1 + * + * @param grid + * @return vector + */ + vector findMissingAndRepeatedValuesOp(vector>& grid) { + int n = grid.size(); + int xor_all = 0; + for (auto& row : grid) { + for (int x : row) { + xor_all ^= x; + } + } + xor_all ^= n % 2 ? 1 : n * n; + int shift = __builtin_ctz(xor_all); + + vector ans(2); + for (int x = 1; x <= n * n; x++) { + ans[x >> shift & 1] ^= x; + } + for (auto& row : grid) { + for (int x : row) { + ans[x >> shift & 1] ^= x; + } + } + + for (auto& row : grid) { + // if (row.find(ans[0]) != row.end()) { + if (std::find(row.begin(), row.end(), ans[0]) != row.end()) { + return ans; + } + } + return {ans[1], ans[0]}; + } + + /** + * @brief LC:248 Strobogrammatic Number III + * Time: O(5^(m/2) + 5^(n/2)) + * Space: O(5^(m/2) + 5^(n/2)) + * + * @param low + * @param high + * @return int + */ + int strobogrammaticInRange(string low, string high) { + int lowLength = low.length(), highLength = high.length(); + if (lowLength > highLength || + (lowLength == highLength && low.compare(high) > 0)) { + return 0; + } + + if (lowLength == highLength) { + auto strobogrammatics = findStrobogrammatic(lowLength); + int lowIndex = binarySearch(strobogrammatics, low); + lowIndex = lowIndex < 0 ? -lowIndex - 1 : lowIndex; + int highIndex = binarySearch(strobogrammatics, high); + highIndex = highIndex < 0 ? -highIndex - 2 : highIndex; + return highIndex - lowIndex + 1; + } else { + int count = 0; + for (int i = lowLength + 1; i < highLength; i++) { + count += countStrobogrammatic(i); + } + auto lowStrobogrammatics = findStrobogrammatic(lowLength); + auto highStrobogrammatics = findStrobogrammatic(highLength); + int lowIndex = binarySearch(lowStrobogrammatics, low); + lowIndex = lowIndex < 0 ? -lowIndex - 1 : lowIndex; + count += lowStrobogrammatics.size() - lowIndex; + int highIndex = binarySearch(highStrobogrammatics, high); + highIndex = highIndex < 0 ? -highIndex - 2 : highIndex; + count += highIndex + 1; + return count; + } + } + + int binarySearch(const vector& list, const string& key) { + int low = 0, high = list.size() - 1; + while (low <= high) { + int mid = (high - low) / 2 + low; + string str = list[mid]; + if (str == key) { + return mid; + } else if (str > key) { + high = mid - 1; + } else { + low = mid + 1; + } + } + return -low - 1; + } + + vector findStrobogrammatic(int n) { + auto findStrobogrammaticWithZero = [&](int n) { + queue queue; + int start = n % 2 == 1 ? 1 : 2; + if (start == 1) { + queue.push("0"); + queue.push("1"); + queue.push("8"); + } else { + queue.push("00"); + queue.push("11"); + queue.push("69"); + queue.push("88"); + queue.push("96"); + } + for (int i = start; i < n; i += 2) { + int size = queue.size(); + for (int j = 0; j < size; j++) { + string prevStrobogrammatic = queue.front(); + queue.pop(); + queue.push("0" + prevStrobogrammatic + "0"); + queue.push("1" + prevStrobogrammatic + "1"); + queue.push("6" + prevStrobogrammatic + "9"); + queue.push("8" + prevStrobogrammatic + "8"); + queue.push("9" + prevStrobogrammatic + "6"); + } + } + vector strobogrammatics; + while (!queue.empty()) { + strobogrammatics.push_back(queue.front()); + queue.pop(); + } + return strobogrammatics; + }; + + auto strobogrammatics = findStrobogrammaticWithZero(n); + if (n == 1) { + return strobogrammatics; + } + strobogrammatics.erase( + remove_if(strobogrammatics.begin(), strobogrammatics.end(), + [](const string& s) { return s[0] == '0'; }), + strobogrammatics.end()); + sort(strobogrammatics.begin(), strobogrammatics.end()); + return strobogrammatics; + } + + int countStrobogrammatic(int n) { + if (n == 1) { + return 3; + } else if (n == 2) { + return 4; + + } else if (n == 3) { + return 12; + } else { + int difference = n % 2 == 0 ? n - 2 : n - 3; + return n % 2 == 0 ? 4 * pow(5, difference / 2) + : 12 * pow(5, difference / 2); + } + } +};