LeetCode icon indicating copy to clipboard operation
LeetCode copied to clipboard

如何实现滑动窗口

Open caipengbo opened this issue 6 years ago • 0 comments

1052. Grumpy Bookstore Owner

实现滑动窗口

    for (int i = 0; i < customers.length; ++i) {
            sum += grumpy[i] == 0 ? customers[i] : 0;
            // 以下两行代码是实现窗口的滑动(窗口内只计算grumpy为1的值)
            currentWindow += grumpy[i] == 1 ? customers[i] : 0;
            if (i >= X) currentWindow -= grumpy[i - X] == 1 ? customers[i - X] : 0;
            maxWindow = Math.max(maxWindow, currentWindow);
        }

caipengbo avatar May 26 '19 06:05 caipengbo