aboutsummaryrefslogtreecommitdiff
path: root/longest_well_perform_int.c
blob: b73aa4ddc51ee81cbe6952ef49d3255a66e284c9 (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
// 1124. Longest Well-Performing Interval
#include "leetcode.h"

/*
 * given 'hours', a list of number of hours worked per day for a given employee.
 * a day is considered to be a tiring day if and only if the number of hours
 * worked is strictly greater than 8. a well performing interval is an interval
 * of days for which the number of tiring days is strictly larger than the
 * number of non tiring days. return the length of the longest well performing
 * interval
 */

int longestWPI(int *hours, int hours_size) {
  int cnt[10001] = {0}, t = 0, max = 0;
  for (int i = 0; i <= hours_size; i++)
    cnt[i] -= 1;
  for (int i = 0; i < hours_size; i++) {
    if (hours[i] > 8)
      t++;
    else
      t--;
    if (t < 0 && cnt[abs(t - 1)] >= 0 && i - cnt[abs(t - 1)] > max)
      max = i - cnt[abs(t - 1)];
  }
  return max;
}

int main() {
  int h1[] = {9, 9, 6, 0, 6, 6, 9}, h2[] = {6, 6, 6};
  printf("%d\n", longestWPI(h1, ARRAY_SIZE(h1))); // expect: 3
  printf("%d\n", longestWPI(h2, ARRAY_SIZE(h2))); // expect: 0
}