当前位置:网站首页>2024. maximum difficulty of the exam - sliding window
2024. maximum difficulty of the exam - sliding window
2022-06-12 08:57:00 【Mr Gao】
2024. The biggest trouble of the exam
A teacher is giving a play by n A test consisting of three judgment questions , The answer to each question is true ( use ‘T’ Express ) perhaps false ( use ‘F’ Express ). Teachers want to increase students' uncertainty about their answers , The method is Maximize Yes Continuous same The number of results .( That is, continuous true Or continuous false).
Give you a string answerKey , among answerKey[i] It's No i The correct result of a question . in addition to , Give you an integer k , Indicates the maximum number of times you can do the following :
In every operation , Change the correct answer to the question to 'T' perhaps 'F' ( Also is to answerKey[i] Change it to 'T' perhaps 'F' ).
Please return no more than k In case of one operation , Maximum continuity ‘T’ perhaps ‘F’ Number of .
Example 1:
Input :answerKey = “TTFF”, k = 2
Output :4
explain : We can put two ‘F’ All become ‘T’ , obtain answerKey = “TTTT” .
There are four consecutive ‘T’ .
Example 2:
Input :answerKey = “TFFT”, k = 1
Output :3
explain : We can put the front ‘T’ Switch to ‘F’ , obtain answerKey = “FFFT” .
perhaps , We can put the second ‘T’ Switch to ‘F’ , obtain answerKey = “TFFF” .
In both cases , There are three consecutive ‘F’ .
Example 3:
Input :answerKey = “TTFTTFTT”, k = 1
Output :5
explain : We can put the first ‘F’ Switch to ‘T’ , obtain answerKey = “TTTTTFTT” .
Or we can put the second ‘F’ Switch to ‘T’ , obtain answerKey = “TTFTTTTT” .
In both cases , There are five consecutive ‘T’ .
Although I did this problem with sliding windows , But it's too complicated , Write your own code , Felt it was very difficult , The solution code is as follows :
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;
}
}
边栏推荐
猜你喜欢

The classic dog contract of smart contract (I)
![[compilation principle] understand BNF](/img/64/9a0e7507606781336fdc44116ba423.jpg)
[compilation principle] understand BNF

Detailed explanation of iSCSI (V) -- actual operation of iSCSI client configuration

QT realizes multi screen and multi-resolution adaptation

Close asymmetric key
![[computer use] how to change a computer disk into a mobile disk?](/img/ff/843f4220fcaefc00980a6edc29aebf.jpg)
[computer use] how to change a computer disk into a mobile disk?
![[character set 9] will GBK be garbled when copied to unicode?](/img/dc/c9ec4a90355d30479f23fdead4b349.png)
[character set 9] will GBK be garbled when copied to unicode?

Engineers learn music theory (II) scale and tendency

机器学习笔记 - 循环神经网络备忘清单

Analysis of 43 cases of MATLAB neural network: Chapter 8 prediction of GRNN Network - Freight Volume Prediction Based on generalized regression neural network
随机推荐
UMI packaging and subcontracting, and compressing to gzip
Introduction to applet cloud development -- questionnaire evaluation applet practice (7)
Code generation tool Autocode for XML Publishing
《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现
调整svg宽高
Specify 404 and 500 error reporting pages.
Introduction to Chang'an chain node certificate, role and authority management
Summary of common character sets
【sklearn学习】LightGBM
Knee joint
Flink传入自定义的参数或配置文件
Set up redis sentinel cluster (instance):
MFS详解(四)——MFS管理服务器安装与配置
【字符集九】gbk拷贝到Unicode会乱码?
Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?
利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
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
Notes used by mqtt (combined with source code)
IP, DNS, domain name, URL, hosts
Does database and table splitting cause reading diffusion problems? How to solve it?