当前位置:网站首页>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;
}
}
边栏推荐
- torch. logical_ And() method
- ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘
- Unittest测试框架
- Shell基本语法--算数运算
- Error: ER_ NOT_ SUPPORTED_ AUTH_ MODE: Client does not support authentication protocol requested ... ...
- Domain name mapping to specified IP
- Background fixing effect
- 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
- 《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现
- [data storage] storage of floating point data in memory
猜你喜欢

Background position - mixed units

Redis installation test
![[compilation principle] understand BNF](/img/64/9a0e7507606781336fdc44116ba423.jpg)
[compilation principle] understand BNF

Detailed explanation of iSCSI (V) -- actual operation of iSCSI client configuration
![[new planning]](/img/8e/0e15e0f3ee08002eaceea1fe8948ec.jpg)
[new planning]

《MATLAB 神經網絡43個案例分析》:第7章 RBF網絡的回歸--非線性函數回歸的實現

The difference between deep copy and shallow copy
![[advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function](/img/90/447d601a8c338cdd5a6674a2dc59ae.png)
[advanced pointer 2] array parameter transfer & pointer parameter transfer & function pointer & function pointer array & callback function

Background location case 1

Close asymmetric key
随机推荐
Knowledge points of 2022 system integration project management engineer examination: project cost management
Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low
Summary of common character sets
Graphic analysis of viewbox in SVG
UMI packaging and subcontracting, and compressing to gzip
[untitled] task3 multiple recall
Binlog in mysql:
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
【字符集九】gbk拷贝到Unicode会乱码?
抓取屏幕与毛玻璃效果
Gets the number of occurrences of a character in a string
Engineers learn music theory (I) try to understand music
域名映射到指定IP
Dynamic segment tree leetcode six hundred and ninety-nine
2022.6.11-----leetcode. nine hundred and twenty-six
IDEA新版UI申请方法+无测试资格使用方法及相关介绍
[advanced pointer I] character array & array pointer & pointer array
Inheritance of row height
Knee joint
IP, DNS, domain name, URL, hosts