leecode 第 62 场双周赛
传送门
第一题 5874. 分割数组的最多方案数
class Solution {
public:
vector> construct2DArray(vector& original, int m, int n) {
int len=original.size();
vector>res(m,vector(n,0));
if(m*n!=len)
{
return {};
}
for(int i=0;i
知识点
vector二维数组的定义。
vectorres(m,vector(n,0))
使用:同数组方法。
第三题 5873. 考试的最大困扰度
class Solution {
public:
int maxConsecutiveAnswers(string answerKey, int k) {
int len=answerKey.size();
if(len==0) return 0;
int l=0,r=0;//滑动窗口[l,r]
int ans=1;//初始化最大长度为1,当前窗口内有一个元素
int tnum=0,fnum=0;
if(answerKey[l]=='T') tnum++;
else fnum++;
while(rk && fnum>k)//窗口内T/F数量超过可替换数量,则左窗口右移
{
if(answerKey[l]=='T') tnum--;
else fnum--;
l++;
}
else //否则,更新最大窗口值,右窗口向右扩大
{
ans=max(ans,(r-l+1));
r++;
if(answerKey[r]=='T') tnum++;
else fnum++;
}
}
return ans;
}
};
第四题 5874. 分割数组的最多方案数
知识点:前缀和+两个哈希表维护
typedef long long LL;
const int maxn=1e5+100;
class Solution {
LL sum[maxn];
unordered_mapmapR,mapL;
public:
int waysToPartition(vector& num, int k) {
LL tot=0;LL ans=0;LL n=num.size();
for(int i=0;i



