-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
2 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |