LeetCode 2379. 得到 K 个黑块的最少涂色次数
class Solution {
public:
int minimumRecolors(string blocks, int k) {
int res=0x3f3f3f3f;
for(int i=0;i+k-1<blocks.size();i++)
{
int l=i,r=i+k-1;
int len=0;
for(int j=l;j<=r;j++)
{
if(blocks[j]=='W')len++;
}
res=min(res,len);
}
return res;
}
};
LeetCode 2380. 二进制字符串重新安排顺序需要的时间
class Solution {
public:
int secondsToRemoveOccurrences(string s) {
int res=0;
while(1)
{
bool f=false;
for(int i=0;i<s.size()-1;i++)
{
if(s[i]=='0'&&s[i+1]=='1')
{
f=true;
s[i]='1',s[i+1]='0';
i++;
}
}
if(f)res++;
else break;
}
return res;
}
};
LeetCode 2381. 字母移位 II
class Solution {
public:
int Mod(int a,int mod){return (a%mod+mod)%mod;}
string shiftingLetters(string s, vector<vector<int>>& shifts) {
int a[50010]={0};
for(int i=0;i<shifts.size();i++)
{
int l=shifts[i][0],r=shifts[i][1],op=shifts[i][2];
if(op==0)a[l]-=1,a[r+1]+=1;
else a[l]+=1,a[r+1]-=1;
}
for(int i=1;i<s.size();i++)a[i]+=a[i-1];
for(int i=0;i<s.size();i++)
{
int x=s[i]-'a'+26+a[i];
s[i]=Mod(x,26)+'a';
}
return s;
}
};
LeetCode 2382. 删除操作后的最大子段和
typedef long long LL;
class Solution {
public:
vector<int>f;
vector<LL>s;
int find(int x)
{
if(f[x]!=x)f[x]=find(f[x]);
return f[x];
}
vector<long long> maximumSegmentSum(vector<int>& nums, vector<int>& removeQueries) {
int n=nums.size();
for(int i=0;i<n;i++)
{
f.push_back(i);
s.push_back(0);
}
vector<LL>res;
LL maxv=0;
for(int i=removeQueries.size()-1;i>=0;i--)
{
int x=removeQueries[i];
s[x]=nums[x];
for(int j=x-1;j<=x+1;j+=2)
{
if(j>=0&&j<n&&s[j]>0)
{
s[x]+=s[find(j)];
f[find(j)]=x;
}
}
res.push_back(maxv);
maxv=max(maxv,s[x]);
}
reverse(res.begin(),res.end());
return res;
}
};