一、题目
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.
Example 1:
Input: hours = [9,9,6,0,6,6,9]
Output: 3
Explanation: The longest well-performing interval is [9,9,6].
Example 2:
Input: hours = [6,6,6]
Output: 0
Constraints:文章来源:https://www.toymoban.com/news/detail-811494.html
1 <= hours.length <= 104
0 <= hours[i] <= 16文章来源地址https://www.toymoban.com/news/detail-811494.html
二、题解
class Solution {
public:
int longestWPI(vector<int>& hours) {
int n = hours.size();
unordered_map<int,int> map;
int res = 0,sum = 0;
map[0] = -1;
for(int i = 0;i < n;i++){
hours[i] > 8 ? sum += 1 : sum += -1;
if(sum > 0) res = i + 1;
else{
if(map.find(sum-1) != map.end()){
res = max(res,i-map[sum-1]);
}
}
if(map.find(sum) == map.end()) map[sum] = i;
}
return res;
}
};
到了这里,关于LeetCode1124. Longest Well-Performing Interval的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!