当前位置:网站首页>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;
}
}
边栏推荐
- POI library update excel picture
- (node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
- node示例后台搭建
- API handling Android security distance
- 2022 simulated examination platform operation of high voltage electrician work license question bank
- 深拷贝与浅拷贝的区别
- 《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
- Implementing architecture caching in MySQL under redis server environment
- Popular understanding of time domain sampling and frequency domain continuation
- Engineers learn music theory (II) scale and tendency
猜你喜欢

【字符集八】char8_t、char16_t、char32_t、wchar、char
![[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?

QT realizes multi screen and multi-resolution adaptation

《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现

Construction of memcached cache service under Linux:

MFS explanation (IV) -- MFS management server installation and configuration

Build personal blog and web.

Handling abnormal data

Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression

Background color translucent
随机推荐
处理异常数据
Dynamic segment tree leetcode six hundred and ninety-nine
(十五) TweenRunner
Introduction to applet cloud development -- questionnaire evaluation applet practice (7)
《MATLAB 神經網絡43個案例分析》:第7章 RBF網絡的回歸--非線性函數回歸的實現
Analysis of 43 cases of MATLAB neural network: Chapter 8 prediction of GRNN Network - Freight Volume Prediction Based on generalized regression neural network
Difference between binary GB and gib
Box model border
[GUI development] browsing function implementation model of image processing software
Shell基本语法--算数运算
Inheritance of row height
[data storage] storage of floating point data in memory
在Tensorflow中把Tensor转换为ndarray时,循环中不断调用run或者eval函数,代码运行越来越慢!
The newline character with in the string is converted to an array
The classic dog contract of smart contract (I)
Graphic analysis of viewbox in SVG
Background location case II
The difference between deep copy and shallow copy
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
Judge whether the object is empty