牛牛的木板

牛牛的木板

https://ac.nowcoder.com/acm/contest/6779/B

class Solution {
public:
    int b[1000006];//第一次出现i个黑色块时的位置
    int tot=0;//目前有多少个黑色块
    int ans=0;//记录答案
    int solve(int n, int m, vector<int>& a) {
        b[0]=-1;//初始化
        for(int i=0;i<n;i++)
        {
            if(a[i]==0)
            {
                tot++;
                b[tot]=i;
                ans=max(ans,i-b[max(0,tot-m)]);//感谢 @Yuanirah 提出的错误
            }
            else{
                ans=max(ans,i-b[max(0,tot-m)]);
            }
        }
        return ans;
    }
};
全部评论
i-b[tot-m]这儿不会出现数组越界吗, 遇到第一个黑色块,tot=1,m>1的时候
1 回复 分享
发布于 2020-08-10 10:19
tql
点赞 回复 分享
发布于 2020-08-06 22:31
看不懂,能简单的讲一下思路吗?
点赞 回复 分享
发布于 2020-08-06 22:06

相关推荐

评论
6
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务