Leetcode 1124:表现良好的最长时间段

题目描述

给你一份工作时间表 hours,上面记录着某一位员工每天的工作小时数。

我们认为当员工一天中的工作小时数大于 8 小时的时候,那么这一天就是「劳累的一天」。

所谓「表现良好的时间段」,意味在这段时间内,「劳累的天数」是严格 大于「不劳累的天数」。

请你返回「表现良好时间段」的最大长度。

示例 1:

输入:hours = [9,9,6,0,6,6,9]
输出:3
解释:最长的表现良好时间段是 [9,9,6]。
 

提示:

1 <= hours.length <= 10000
0 <= hours[i] <= 16

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-well-performing-interval
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

扫描二维码关注公众号,回复: 9363041 查看本文章

解题思路

class Solution {
public:
    int i,j,len,res;
    int presum[10001];
    int longestWPI(vector<int>& hours) {
        len = hours.size();
        for(i=1;i<=len;i++) presum[i] = presum[i-1] + (hours[i-1]>8?1:-1);
        for(i=1;i<=len;i++) {
            for(j=0;j<i;j++) if(presum[i]-presum[j]>0) break;
            res = max(res,i-j);
        }
        return res;
    }
};
发布了598 篇原创文章 · 获赞 10 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_35338624/article/details/104207325