aboutsummaryrefslogtreecommitdiff
path: root/grumpy_bookstore_owner.c
blob: 69330ca79c0e435cae0f61b50d3bf36e4d1174ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 1052. Grumpy Bookstore Owner
#include "leetcode.h"

/*
 * there is a bookstore owner that has a store open for 'n' miniutes. every
 * minute some number of customers entre the store. you are given an integer
 * array 'customers' of size 'n' where 'customers[i]' is the number of the
 * customer that entres the store at the start of the i'th minute and all those
 * customers leave after the end of that minute. on some minutes, the bookstore
 * owner is grumpy. you are given an binary array 'grumpy' where 'grumpy[i]' is
 * 1 if the bookstore owner is grumpy and zero otherwise. return the maximum
 * number of customers that can be satisfied throughout the day.
 */

int maxSatisfied(int *customers, int customersSize, int *grumpy, int grumpySize,
                 int minutes) {
  int total_satsified = 0, additional_satisfied = 0, curr = 0;
  for (int i = 0; i < customersSize; i++)
    if (!grumpy[i])
      total_satsified += customers[i];
  for (int i = 0; i < minutes; i++)
    if (grumpy[i])
      curr += customers[i];
  additional_satisfied = curr;
  for (int i = minutes; i < customersSize; i++) {
    if (grumpy[i])
      curr += customers[i];
    if (grumpy[i - minutes])
      curr -= customers[i - minutes];
    if (curr > additional_satisfied)
      additional_satisfied = curr;
  }
  return total_satsified + additional_satisfied;
}

int main() {
  int c1[] = {1, 0, 1, 2, 1, 1, 7, 5}, g1[] = {0, 1, 0, 1, 0, 1, 0, 1},
      c2[] = {1}, g2[] = {0};
  printf("%d\n",
         maxSatisfied(c1, ARRAY_SIZE(c1), g1, ARRAY_SIZE(c1), 3)); // expect: 16
  printf("%d\n",
         maxSatisfied(c2, ARRAY_SIZE(c2), g2, ARRAY_SIZE(c2), 1)); // expect: 1
}