add: 220329-CN [cpp]

This commit is contained in:
Lam Haoyin 2022-03-29 22:45:10 +08:00
parent dbeda21c92
commit bbe37d1fa7
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
2 changed files with 35 additions and 1 deletions

34
cpp/2203/220329-CN.cpp Normal file
View File

@ -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;
}

View File

@ -3,4 +3,4 @@ PROJECT(2203)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2203 220329.cpp)
ADD_EXECUTABLE(2203 220329-CN.cpp)