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.
Example 1:
Thoughts
一串数字,找其中满足值大于8元素个数大于不满足的个数的最长子数组。
元素分成两种,并且要比较两种的个数,想到可以+1-1且当子数组sum>0时说明该子数组为满足条件的数组。
答案分成两类,整个数组即答案,此时sum(v[0:N]) > 0; 或者是子数组[i:j],此时一定满足sum[v[i:j]] == 1, 因为如果>1,说明该子数组还可以扩张来包含-1的元素,不会是最优解。
找满足sum(v[i:j]) == k的最长子数组,转化成max subarray equals k 问题,用persum求解(hashmap)。
Last updated
Was this helpful?