From 132868f33efe0e01612892ba500c9f4202f34c74 Mon Sep 17 00:00:00 2001 From: aucker Date: Mon, 2 Sep 2024 08:53:19 +0800 Subject: [PATCH] Sep2: daily update, Sliding window [M] impl of sliding window, Time: O(N), Space: O(1) try to familize the bit operation, bit operation is fast, which is widely used in hardware programming. --- daily/Aug31.cc | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ daily/Sep2.cc | 25 ++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 daily/Aug31.cc create mode 100644 daily/Sep2.cc diff --git a/daily/Aug31.cc b/daily/Aug31.cc new file mode 100644 index 0000000..aadbc2d --- /dev/null +++ b/daily/Aug31.cc @@ -0,0 +1,79 @@ +#include +using namespace std; + +class Solution { + public: + bool canMakeSquare(vector>& grid) { + /** + * B B B . B w b + * w w w . w b w + * b b b b w b b + */ + int cnt_w = 0; + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + if (grid[i][j] == 'W') { + cnt_w ++; + } + } + } + if (cnt_w != 2) return true; + cnt_w = 0; + + for (int i = 0; i < 2; i++) { + for (int j = 1; j < 3; j++) { + if (grid[i][j] == 'W') { + cnt_w ++; + } + } + } + if (cnt_w != 2) return true; + cnt_w = 0; + + for (int i = 1; i < 3; i++) { + for (int j = 0; j < 2; j++) { + if (grid[i][j] == 'W') { + cnt_w ++; + } + } + } + if (cnt_w != 2) return true; + cnt_w = 0; + + for (int i = 1; i < 3; i++) { + for (int j = 1; j < 3; j++) { + if (grid[i][j] == 'W') { + cnt_w ++; + } + } + } + if (cnt_w != 2) return true; + + return false; + } + + /** + * @brief 138th weekly Q1: key of numbers [E] + * basic number ops, very easy + * + * @param num1 num2 num3 + * @return int + */ + int generateKey(int num1, int num2, int num3) { + int ans = 0; + int flag = 1; + while (num1 >= 10 || num2 >= 10 || num3 >= 10) { + int n1 = num1 % 10; + int n2 = num2 % 10; + int n3 = num3 % 10; + num1 /= 10; + num2 /= 10; + num3 /= 10; + ans = ans + flag * min(min(n1, n2), n3); + flag *= 10; + } + ans = ans + flag * min(min(num1, num2), num3); + return ans; + } + +}; diff --git a/daily/Sep2.cc b/daily/Sep2.cc new file mode 100644 index 0000000..9a24e2f --- /dev/null +++ b/daily/Sep2.cc @@ -0,0 +1,25 @@ +#include +using namespace std; + +class Solution { + public: + /** + * @brief LC2024: maximize the confusion of exam [M] + * Sliding window: Time: O(N), Space: O(1) + * + * @param answerKey + * @param k + * @return int + */ + int maxConsecutiveAnswers(string answerKey, int k) { + int ans = 0, le = 0, cnt[2]{}; + for (int ri = 0; ri < answerKey.length(); ri++) { + cnt[answerKey[ri] >> 1 & 1]++; + while (cnt[0] > k & cnt[1] > k) { + cnt[answerKey[le++] >> 1 & 1]--; + } + ans = max(ans, ri - le + 1); + } + return ans; + } +};