LeetCode1124. Longest Well-Performing Interval

该篇文章介绍了一道编程问题,给定员工每天的工作小时数,目标是找到工作日中疲劳天数多于非疲劳天数的最长连续区间。解决方案使用了哈希映射来跟踪工作状态和区间长度。
摘要由CSDN通过智能技术生成

一、题目

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:

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

二、题解

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;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值