add: 220329-CN [cpp]
This commit is contained in:
parent
dbeda21c92
commit
bbe37d1fa7
|
|
@ -0,0 +1,34 @@
|
||||||
|
#include <string>
|
||||||
|
#include <functional>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
static int maxConsecutiveAnswers(const std::string& answerKey, int k) {
|
||||||
|
int n = answerKey.length();
|
||||||
|
std::function<int(char)> solve = [&](char ch) {
|
||||||
|
int L = 0, R = 0, cnt = 0;
|
||||||
|
// Interval: [L, R)
|
||||||
|
// Initial
|
||||||
|
for (; R < n && cnt < k; ++R)
|
||||||
|
if (answerKey[R] != ch)
|
||||||
|
++cnt;
|
||||||
|
int ret = R - L;
|
||||||
|
for (; R < n; ++R) {
|
||||||
|
if (answerKey[R] != ch) {
|
||||||
|
ret = std::max(ret, R - L);
|
||||||
|
while (answerKey[L] == ch)
|
||||||
|
++L;
|
||||||
|
++L;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::max(ret, R - L);
|
||||||
|
};
|
||||||
|
return std::max(solve('T'), solve('F'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::cout << Solution::maxConsecutiveAnswers("TFFT", 1);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2203)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2203 220329.cpp)
|
ADD_EXECUTABLE(2203 220329-CN.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue