zoukankan      html  css  js  c++  java
  • 1052. Grumpy Bookstore Owner

    Today, the bookstore owner has a store open for customers.length minutes.  Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.

    On some minutes, the bookstore owner is grumpy.  If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0.  When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.

    The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.

    Return the maximum number of customers that can be satisfied throughout the day.

    Example 1:

    Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
    Output: 16
    Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes. 
    The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
    

    Note:

    • 1 <= X <= customers.length == grumpy.length <= 20000
    • 0 <= customers[i] <= 1000
    • 0 <= grumpy[i] <= 1
    class Solution {
        public int maxSatisfied(int[] customers, int[] grumpy, int X) {
            int n = customers.length;
            int res = 0, left = 0, curmax = 0, max = 0;
            int[] window = new int[n];
            
            for(int right = 0; right < n; right++) {
                if(grumpy[right] == 1) {
                    window[right] = customers[right];
                    curmax += customers[right];
                }
                else {
                    window[right] = 0;
                    res += customers[right];
                }
                if(right >= X) {
                    curmax -= window[left];
                    left++;
                }
                max = Math.max(curmax, max);
            }
            return max + res;
        }
    }

    sliding window,max是window中最大的grumpy customers,res是非grumpy(不受影响)

    class Solution {
        public int maxSatisfied(int[] customers, int[] grumpy, int X) {
            int satis = 0, maxwin = 0, curwin = 0;
            for(int i = 0; i < customers.length; i++) {
                if(grumpy[i] == 0) satis += customers[i];
                else {
                    curwin += customers[i];
                }
                if(i >= X) curwin -= grumpy[i - X] * customers[i - X];
                maxwin = Math.max(curwin, maxwin);
            }
            return satis + maxwin;
        }
    }

    就不用array了,拉跨

    因为window大小固定了,所以当i 》= X时,left就是 i - X, right就是i。所以grumpy == 0就加到satis,==1就加到当前的window。如果超了window就尝试更新window里最大的值

  • 相关阅读:
    HDOJ 2095 find your present (2)
    HDOJ 2186 悼念512汶川大地震遇难同胞——一定要记住我爱你
    九度 1337 寻找最长合法括号序列
    九度 1357 疯狂地Jobdu序列
    HDOJ 1280 前m大的数
    九度 1343 城际公路网
    九度 1347 孤岛连通工程
    HDOJ 2151 Worm
    九度 1342 寻找最长合法括号序列II
    九度 1346 会员积分排序
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13493872.html
Copyright © 2011-2022 走看看