当前位置:网站首页>2024. 考试的最大困扰度-滑动窗口
2024. 考试的最大困扰度-滑动窗口
2022-06-12 08:56:00 【Mr Gao】
2024. 考试的最大困扰度
一位老师正在出一场由 n 道判断题构成的考试,每道题的答案为 true (用 ‘T’ 表示)或者 false (用 ‘F’ 表示)。老师想增加学生对自己做出答案的不确定性,方法是 最大化 有 连续相同 结果的题数。(也就是连续出现 true 或者连续出现 false)。
给你一个字符串 answerKey ,其中 answerKey[i] 是第 i 个问题的正确结果。除此以外,还给你一个整数 k ,表示你能进行以下操作的最多次数:
每次操作中,将问题的正确答案改为 'T' 或者 'F' (也就是将 answerKey[i] 改为 'T' 或者 'F' )。
请你返回在不超过 k 次操作的情况下,最大 连续 ‘T’ 或者 ‘F’ 的数目。
示例 1:
输入:answerKey = “TTFF”, k = 2
输出:4
解释:我们可以将两个 ‘F’ 都变为 ‘T’ ,得到 answerKey = “TTTT” 。
总共有四个连续的 ‘T’ 。
示例 2:
输入:answerKey = “TFFT”, k = 1
输出:3
解释:我们可以将最前面的 ‘T’ 换成 ‘F’ ,得到 answerKey = “FFFT” 。
或者,我们可以将第二个 ‘T’ 换成 ‘F’ ,得到 answerKey = “TFFF” 。
两种情况下,都有三个连续的 ‘F’ 。
示例 3:
输入:answerKey = “TTFTTFTT”, k = 1
输出:5
解释:我们可以将第一个 ‘F’ 换成 ‘T’ ,得到 answerKey = “TTTTTFTT” 。
或者我们可以将第二个 ‘F’ 换成 ‘T’ ,得到 answerKey = “TTFTTTTT” 。
两种情况下,都有五个连续的 ‘T’ 。
这题虽然我也是用滑动窗口做的,但是过于复杂,自己写代码,都觉得很费劲,解题代码如下:
int maxConsecutiveAnswers(char * answerKey, int k){
int n=0;
int sum=0;
int max=0;
int i=0;
k=k+1;
int r[k];
for(i=0;i<k;i++){
r[i]=0;
}
int num=0;
int knum=0;
i=0;
int j;
while(answerKey[i]!='\0'){
if(answerKey[i]=='F'){
if(knum<k-1){
r[knum]=num+1;
max=num+1+max;
sum=num+1+sum;
}
else{
if(knum==k-1){
max=num+max;
r[knum]=num+1;
sum=max;
}
if(knum>k-1){
sum=sum+num+1-r[knum%k];
if(sum>max){
max=sum;
}
r[knum%k]=num+1;
}
}
num=0;
knum++;
}
else{
num++;
}
i++;
}
if(answerKey[i-1]=='T'){
if(knum<=k-1){
max=max+num;
}
else{
sum=sum+num+1-r[knum%k];
if(sum>max){
max=sum;
}
}
}
num=0;
knum=0;
for(i=0;i<k;i++){
r[i]=0;
}
i=0;
int max1=0;
sum=0;
while(answerKey[i]!='\0'){
if(answerKey[i]=='T'){
if(knum<k-1){
r[knum]=num+1;
max1=num+1+max1;
sum=num+1+sum;
}
else{
if(knum==k-1){
max1=num+max1;
r[knum]=num+1;
sum=max1;
}
if(knum>k-1){
sum=sum+num+1-r[knum%k];
if(sum>max1){
max1=sum;
}
r[knum%k]=num+1;
}
}
num=0;
knum++;
}
else{
num++;
}
i++;
}
if(answerKey[i-1]=='F'){
if(knum<=k-1){
max1=max1+num;
}
else{
sum=sum+num+1-r[knum%k];
if(sum>max1){
max1=sum;
}
}
}
if(max1>max){
return max1;
}
else{
return max;
}
}
边栏推荐
猜你喜欢

Background location case 1

Notes used by mqtt (combined with source code)

Priority issues

Unittest测试框架

Background position position NOUN

2022 simulated examination platform operation of high voltage electrician work license question bank

Set up redis sentinel cluster (instance):

Union selector

利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包

【字符集七】汉字的宽字符码和多字节码分别是多少
随机推荐
Convert spaces to < br / > newline labels
Loading circling effect during loading
第四章-第一个程序
深拷贝与浅拷贝的区别
The newline character with in the string is converted to an array
torch. logical_ And() method
【sklearn学习】LightGBM
Construction of memcached cache service under Linux:
(十五) TweenRunner
判断对象是否为空
Background position - exact units
Shell基本语法--数组
【无标题】Task3 多路召回
最少换乘次数
You have an error in your SQL syntax; use near ‘and title=‘xxx‘‘ at line 5
43 cas d'analyse du réseau neuronal MATLAB: chapitre 7 régression du réseau RBF - - réalisation de la régression fonctionnelle non linéaire
POI library update excel picture
Redis installation test
[computer use] how to change a computer disk into a mobile disk?
【数据存储】浮点型数据在内存中的存储