[LeetCode] Longest Well-Performing Interval

1124. Longest Well-Performing Interval

We are given hours, a list of the 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
int longestWPI(vector<int>& hours) {
int res = 0;
int sum = 0;
unordered_map<int, int> table;
for(int i = 0; i < hours.size(); i++) {
sum += (hours[i] >= 9 ? 1 : -1);
if(sum > 0) {
res = i + 1;
} else {
if(!table.count(sum))
table[sum] = i;

if(table.count(sum - 1))
res = max(res, i - table[sum - 1]);
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2021/02/24/PS/LeetCode/longest-well-performing-interval/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.