problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::maximize_the_confusion_of_an_exam::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static int maxConsecutiveAnswers (string answerKey, int k)
 

详细描述

在文件 leetcode.h1966 行定义.

成员函数说明

◆ maxConsecutiveAnswers()

int leetcode::maximize_the_confusion_of_an_exam::Solution::maxConsecutiveAnswers ( string  answerKey,
int  k 
)
static

在文件 leetcode.cpp5214 行定义.

5214 {
5215 if(answerKey.length() == 1) {
5216 return 1;
5217 }
5218 vector<int> t_count(answerKey.length());
5219 vector<int> f_count(answerKey.length());
5220 int t_current = 0;
5221 int f_current = 0;
5222 for(int i = 0; i < t_count.size(); i++) {
5223 if(answerKey[i] == 'T') {
5224 t_current++;
5225 } else {
5226 f_current++;
5227 }
5228 t_count[i] = t_current;
5229 f_count[i] = f_current;
5230 }
5231 int l = 1;
5232 int r = answerKey.size();
5233 auto check = [&answerKey, &t_count, &f_count, &k](int len) -> bool {
5234 for(int i = 0; i + len <= answerKey.length(); i++) {
5235 int tc = t_count[i + len - 1] - (i - 1 >= 0 ? t_count[i - 1] : 0);
5236 int fc = f_count[i + len - 1] - (i - 1 >= 0 ? f_count[i - 1] : 0);
5237 if(min(tc, fc) <= k) {
5238 return true;
5239 }
5240 }
5241 return false;
5242 };
5243 while(l < r) {
5244 if(l + 1 == r) {
5245 if(check(r)) {
5246 return r;
5247 }
5248 return l;
5249 }
5250 const int mid = (l + r) / 2;
5251 if(check(mid)) {
5252 l = mid;
5253 } else {
5254 r = mid;
5255 }
5256 }
5257 return -1;
5258 }

被这些函数引用 leetcode::maximize_the_confusion_of_an_exam::TEST().


该类的文档由以下文件生成: