【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 ...
随机推荐
- 通过 vSphere WS API 获取 vCenter Datastore Provisioned Space 置备空间
目录 文章目录 目录 Provisioned Space & Used Space Provisioned Space 的计算方式 Uncommitted Space 扩展:置备率的计算公式 ...
- Linux_Grub2、系统启动流程_RHEL7
目录 目录 前言 系统启动流程 控制RHEL7启动过程 编辑gurbcfg RHEL7启动级别 修改系统运行级别 RHEL7破密码步骤 grup2加密防止破密码 initramfs文件 前言 RHEL ...
- jmeter常用性能监听器分析
jmeter中提供了很多性能数据的监听器,我们通过监听器可以来分析性能瓶颈 本文以500线程的阶梯加压测试结果来描述图表. 常用监听器 1:Transactions per Second 监听动态TP ...
- cocos2dx基础篇(17) 音乐音效SimpleAudioEngine
[3.x] (1)获取单例:sharedEngine() 改为 getInstance() (2)实现了:音量的调节. (3)修改了播放音效 playEffect() 的参数: ...
- Hello World!----html
最近要做一个小网站,今晚想起来还是先看看前端终于抑制住惰性,开始看了. 看了一下html,写了个hello world.老实讲,我竟然还有些小激动 <html> <hea ...
- ssh远程连接linux服务器并执行命令
详细方法: SSHClient中的方法 参数和参数说明 connect(实现ssh连接和校验) hostname:目标主机地址 port:主机端口 username:校验的用户名 password:登 ...
- ant buid.xml 模板
<?xml version="1.0" encoding="UTF-8"?> <project name="ant" de ...
- 1、Java语言概述与开发环境——Java程序运行机制
Java语言是一种特殊的高级语言,它既有解释型语言的特性,也具有编译型语言的特征,因为Java要经过先编译后解释两个步骤. 一.高级语言的运行机制 计算机高级语言按程序的执行方式可以分为编译型和解释型 ...
- CQRS框架(nodejs的DDD开发落地框架)初识感想
CQRS是啥?DDD又是啥? 这两个概念其实没什么神秘的,当然此文章中的这两个概念以曾老师的课程为准(关于CQRS和DDD的标准概念,google上已经很多了,不再赘述.) DDD(Domain Dr ...
- 利用AXI VDMA实现OV5640摄像头采集
利用AXI VDMA实现OV5640摄像头采集 导读:摄像头采样图像数据后经过VDMA进入DDR,通过PS部分控制,经过三级缓存,将DDR中保持的图形数据通过VDMA发送出去.在FPGA的接收端口产生 ...