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

思路:>8的转化为1,<8的转化为-1

于是题目就变成求最长的subarray,使得该subarray的和大于0

如果是等于0,直接hashtable查找就行

大于0需要变一下,还是做从0开始的running sum,用hashtable记录负的sum,

假设遍历到index位置,从0开始的running sum为-1,那就从hashtable里面找-2(所以存的时候-2一定要存数组索引最小的,转化到代码就是如果-2已经在hashtable就不更新),这样中间的区间sum就为1

为什么不找-3,-4呢?因为他们不会出现在-2的前面(-2过了才能减到-3,而-2是最优的)

 

class Solution(object):
    def longestWPI(self, hours):
        """
        :type hours: List[int]
        :rtype: int
        """
        helper = [1 if a>8 else -1 for a in hours]
        d = {}
        res = 0
        running = 0
        for i,a in enumerate(helper):
            running += a
            if running>0:
                res=max(res,i+1)
            else:
                if running-1 in d:
                    res=max(res,i-d[running-1])
                if running not in d: d[running]=i
                
        return res
    

 

function pdemodel [pde_fig,ax]=pdeinit; pdetool('appl_cb',5); set(ax,'DataAspectRatio',[1 1 1]); set(ax,'PlotBoxAspectRatio',[1.5 1 1]); set(ax,'XLim',[-1.5 1.5]); set(ax,'YLim',[-1 1]); set(ax,'XTick',[ -1.5,... -1.2,... -0.90000000000000002,... -0.60000000000000009,... -0.30000000000000004,... 0,... 0.30000000000000004,... 0.60000000000000009,... 0.90000000000000002,... 1.2,... 1.5,... ]); set(ax,'YTickMode','auto'); pdetool('gridon','on'); % Geometry description: pderect([-1 0.5 0.5 -0.5],'R1'); set(findobj(get(pde_fig,'Children'),'Tag','PDEEval'),'String','R1') % Boundary conditions: pdetool('changemode',0) pdesetbd(4,... 'dir',... 1,... '1',... '0') pdesetbd(3,... 'dir',... 1,... '1',... '0') pdesetbd(2,... 'dir',... 1,... '1',... '0') pdesetbd(1,... 'dir',... 1,... '1',... '200') % Mesh generation: setappdata(pde_fig,'Hgrad',1.3); setappdata(pde_fig,'refinemethod','regular'); setappdata(pde_fig,'jiggle',char('on','mean','')); setappdata(pde_fig,'MesherVersion','preR2013a'); pdetool('initmesh') % PDE coefficients: pdeseteq(1,... '1.0',... '0.0',... '0',... '1.0',... '0:10',... '0.0',... '0.0',... '[0 100]') setappdata(pde_fig,'currparam',... ['1.0';... '0 ']) % Solve parameters: setappdata(pde_fig,'solveparam',... char('0','1000','10','pdeadworst',... '0.5','longest','0','1E-4','','fixed','Inf')) % Plotflags and user data strings: setappdata(pde_fig,'plotflags',[2 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 0 1]); setappdata(pde_fig,'colstring',''); setappdata(pde_fig,'arrowstring',''); setappdata(pde_fig,'deformstring',''); setappdata(pde_fig,'heightstring',''); % Solve PDE: pdetool('solve')
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值