给你一份工作时间表 hours
,上面记录着某一位员工每天的工作小时数。
我们认为当员工一天中的工作小时数大于 8
小时的时候,那么这一天就是「劳累的一天」。
所谓「表现良好的时间段」,意味在这段时间内,「劳累的天数」是严格 大于「不劳累的天数」。
请你返回「表现良好时间段」的最大长度。
示例 1:
输入:hours = [9,9,6,0,6,6,9] 输出:3 解释:最长的表现良好时间段是 [9,9,6]。
提示:
1 <= hours.length <= 10000
0 <= hours[i] <= 16
class Solution:
def longestWPI(self, hours: List[int]) -> int:
pre_sum, res = 0, 0
mp = {}
for i in range(len(hours)):
temp = 1 if hours[i] > 8 else -1
pre_sum += temp
if pre_sum > 0:
res = i + 1
else:
if pre_sum not in mp:
mp[pre_sum] = i
if (pre_sum - 1) in mp:
res = max(res, i - mp[pre_sum - 1])
return res
class Solution {
public int longestWPI(int[] hours) {
int res = 0;
Map<Integer, Integer> map = new HashMap<>();
int s = 0;
for (int i = 0; i < hours.length; ++i) {
s += hours[i] > 8 ? 1 : -1;
if (s > 0) {
res = i + 1;
} else {
int b = map.getOrDefault(s - 1, -1);
if (b != -1) {
res = Math.max(res, i - b);
}
}
map.putIfAbsent(s, i);
}
return res;
}
}