zoukankan      html  css  js  c++  java
  • 【leetcode】1124. 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.

    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].
    

    Constraints:

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

    解题思路:假设i~j (j不是hours中最后一个元素) 区间是最长符合条件的时间段,那么必定在这个区间内大于8的元素的总数减去小于8的元素的总数的值为1。这里可以通过反证法,如果差值大于1,那么无论第j+1的元素大于还是小于8,i~j+1区间内大于8的元素总数必定是大于小于8的元素的总数的。知道了这个规律就很好办了,遍历hours数组,记录从0开始的0~i区间内大于8的元素的总数与小于8的元素的总数的差值,并记为val[i],如果j元素是最长区间内最后一个元素,那么只要找到第一个i满足 val[j] - val[i] = 1即可,再判断一下val[j]是否大于0,如果大于0则表示考虑到最长区间是0~j。最后一步再针对最后一个元素单独计算依次即可。

    代码如下:

    class Solution(object):
        def longestWPI(self, hours):
            """
            :type hours: List[int]
            :rtype: int
            """
            dic = {}
            count = 0
            res = 0
            for i in range(len(hours)-1):
                count = count + 1 if hours[i] > 8 else count - 1
                if count not in dic:
                    dic[count] = i
                if count - 1 in dic:
                    res = max(res,i - dic[count - 1])
                if count == 1:
                    res = max(res,i + 1)
    
            # the last
            count = 0
            for i in range(len(hours) - 1,-1,-1):
                count = count + 1 if hours[i] > 8 else count - 1
                if count > 0: res = max(res, len(hours) - i)
    
    
            return res
  • 相关阅读:
    UIAlertView
    网络请求ASIhttp
    省份城市选择
    Certificates
    UTF8
    xcode增加注释插件
    常用片段 button Label
    开发铺助工具
    iOS UI框架
    iOS 引导页
  • 原文地址:https://www.cnblogs.com/seyjs/p/11385853.html
Copyright © 2011-2022 走看看