|
| 1 | +//Problem Name: Grumpy Bookstore owner |
| 2 | +//Problem Link: https://leetcode.com/problems/grumpy-bookstore-owner/ |
| 3 | + |
| 4 | + |
| 5 | +//Code: |
| 6 | + |
| 7 | +class Solution { |
| 8 | +public: |
| 9 | + int maxSatisfied(vector<int>& customers, vector<int>& grumpy, int minutes) { |
| 10 | + int satisfied=0; |
| 11 | + int n= customers.size(); |
| 12 | + for(int i=0;i<n;i++){ |
| 13 | + if(grumpy[i]==0) |
| 14 | + satisfied+= customers[i]; |
| 15 | + } |
| 16 | + int additionalsatisfied= 0; |
| 17 | + int left=0; |
| 18 | + int right= minutes; |
| 19 | + for(int i=0;i<minutes;i++){ |
| 20 | + if(grumpy[i]==1) |
| 21 | + { |
| 22 | + additionalsatisfied+= customers[i]; |
| 23 | + } |
| 24 | + } |
| 25 | + int maxx= additionalsatisfied; |
| 26 | + while(right<n){ |
| 27 | + if(grumpy[right]==1){ |
| 28 | + additionalsatisfied+= customers[right]; /* If the satisfied customer found, then include in the given window or minute.*/ |
| 29 | + } |
| 30 | + if(grumpy[left]==1){ |
| 31 | + additionalsatisfied-= customers[left]; /*If the unsatisfied customers found, then exclude from the window*/ |
| 32 | + } |
| 33 | + maxx= max(maxx, additionalsatisfied); |
| 34 | + left++; |
| 35 | + right++; |
| 36 | + } |
| 37 | + |
| 38 | + return satisfied+maxx; |
| 39 | + } |
| 40 | +}; |
0 commit comments