Skip to content

Commit

Permalink
Sep2: daily update, Sliding window [M]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aucker committed Sep 2, 2024
1 parent e724566 commit 132868f
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
79 changes: 79 additions & 0 deletions daily/Aug31.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <iostream>
using namespace std;

class Solution {
public:
bool canMakeSquare(vector<vector<char>>& 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;
}

};
25 changes: 25 additions & 0 deletions daily/Sep2.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <iostream>
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;
}
};

0 comments on commit 132868f

Please sign in to comment.