Skip to content

Commit 132868f

Browse files
committed
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.
1 parent e724566 commit 132868f

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

daily/Aug31.cc

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
bool canMakeSquare(vector<vector<char>>& grid) {
7+
/**
8+
* B B B . B w b
9+
* w w w . w b w
10+
* b b b b w b b
11+
*/
12+
int cnt_w = 0;
13+
for (int i = 0; i < 2; i++) {
14+
for (int j = 0; j < 2; j++) {
15+
if (grid[i][j] == 'W') {
16+
cnt_w ++;
17+
}
18+
}
19+
}
20+
if (cnt_w != 2) return true;
21+
cnt_w = 0;
22+
23+
for (int i = 0; i < 2; i++) {
24+
for (int j = 1; j < 3; j++) {
25+
if (grid[i][j] == 'W') {
26+
cnt_w ++;
27+
}
28+
}
29+
}
30+
if (cnt_w != 2) return true;
31+
cnt_w = 0;
32+
33+
for (int i = 1; i < 3; i++) {
34+
for (int j = 0; j < 2; j++) {
35+
if (grid[i][j] == 'W') {
36+
cnt_w ++;
37+
}
38+
}
39+
}
40+
if (cnt_w != 2) return true;
41+
cnt_w = 0;
42+
43+
for (int i = 1; i < 3; i++) {
44+
for (int j = 1; j < 3; j++) {
45+
if (grid[i][j] == 'W') {
46+
cnt_w ++;
47+
}
48+
}
49+
}
50+
if (cnt_w != 2) return true;
51+
52+
return false;
53+
}
54+
55+
/**
56+
* @brief 138th weekly Q1: key of numbers [E]
57+
* basic number ops, very easy
58+
*
59+
* @param num1 num2 num3
60+
* @return int
61+
*/
62+
int generateKey(int num1, int num2, int num3) {
63+
int ans = 0;
64+
int flag = 1;
65+
while (num1 >= 10 || num2 >= 10 || num3 >= 10) {
66+
int n1 = num1 % 10;
67+
int n2 = num2 % 10;
68+
int n3 = num3 % 10;
69+
num1 /= 10;
70+
num2 /= 10;
71+
num3 /= 10;
72+
ans = ans + flag * min(min(n1, n2), n3);
73+
flag *= 10;
74+
}
75+
ans = ans + flag * min(min(num1, num2), num3);
76+
return ans;
77+
}
78+
79+
};

daily/Sep2.cc

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
/**
7+
* @brief LC2024: maximize the confusion of exam [M]
8+
* Sliding window: Time: O(N), Space: O(1)
9+
*
10+
* @param answerKey
11+
* @param k
12+
* @return int
13+
*/
14+
int maxConsecutiveAnswers(string answerKey, int k) {
15+
int ans = 0, le = 0, cnt[2]{};
16+
for (int ri = 0; ri < answerKey.length(); ri++) {
17+
cnt[answerKey[ri] >> 1 & 1]++;
18+
while (cnt[0] > k & cnt[1] > k) {
19+
cnt[answerKey[le++] >> 1 & 1]--;
20+
}
21+
ans = max(ans, ri - le + 1);
22+
}
23+
return ans;
24+
}
25+
};

0 commit comments

Comments
 (0)