【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.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].Constraints:
1 <= hours.length <= 100000 <= 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
【leetcode】1124. Longest Well-Performing Interval的更多相关文章
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
- 【LeetCode】159. Longest Substring with At Most Two Distinct Characters
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- 【leetcode】300.Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
- 【leetcode】424. Longest Repeating Character Replacement
题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...
- 【LeetCode】409. Longest Palindrome 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:HashSet 方法三 ...
- 【LeetCode】845. Longest Mountain in Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双数组 参考资料 日期 题目地址:https://l ...
- 【LeetCode】720. Longest Word in Dictionary 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力查找 排序 日期 题目地址:https://le ...
- 【LeetCode】14. Longest Common Prefix 最长公共前缀
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣 ...
- 【LeetCode】594. Longest Harmonious Subsequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计次数 日期 题目地址:https://leetc ...
随机推荐
- 阶段3 1.Mybatis_06.使用Mybatis完成DAO层的开发_3 Mybatis中编写dao实现类的使用-修改删除等其他操作
update和上面的Insert代码基本是一样的,只需要修改这里, 测试Update的方法 删除 findById 测试方法 findByName 测试方法 findTotal
- python学习笔记:(五)列表与元组的异同
在python中最基本的数据结构是序列(sequence),每一个元素被分配一个序号,即元素的位置,也称为索引,第一个索引是0,第二个则是1 元组与列表最大的区别就是: 元组不能更改:列表可以修改 p ...
- Python学习之==>操作MySQL
一.简介: MySQL为关系型数据库,其他关系型数据库包括Oracle.DB2.Sql Server等等.Python操作MySQL需要使用到pymsyql模块,pip安装即可. 二.操作MySQL步 ...
- wpf slider刻度
TickFrequency:刻度之间的间隔 IsSnapToTickEnabled:是否对齐到刻度 TickPlacement:刻度位置
- Oracle 查询库文件信息
--.查看Oracle数据库中数据文件信息的命令方法 select b.file_name 物理文件名, b.tablespace_name 表空间, b.bytes// 大小M, (b.bytes- ...
- windows下重启nginx
参考:从零学nginx-windows下reload配置无效及如何重启 因为Nginx是多进程模型,有一个主进程和多个子进程,主进程只负责管理子进程,基本的网络事件由各个子进程处理. 所以有时候当我们 ...
- Luogu P1315 [NOIP2012]观光公交
题目 每次把加速器用在可以是答案减少最多的地方就即可.(这不是废话吗?) 具体而言,我们处理出: \(sum_i\)到\(i\)为止下车人数之和. \(t_i\)在\(i\)最晚的上车的人的上车时间. ...
- 数学: HDU1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- react找到对象数组中指定的值
找到对象数组中指定的值var array = [ { label: "Custom", value: "0" }, ...
- debian利用snmp监控服务器异常状态
1.安装snmp apt-get install snmp snmpd 2.配置snmp vi /etc/snmp/snmpd.conf 注释15行 #agentAddress udp:127.0.0 ...