【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 <= 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
【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 ...
随机推荐
- 【漏洞学习】slowHTTPtest 慢速 DOS 攻击方法 修复方案
日期:2018-05-28 21:41:59 更新:2019-07-05 23:15:21 作者:Bay0net 介绍:学习一下 slowHTTPtest 的攻击及防御. 0x01. 安装 下载链接 ...
- 现有1~100 共一百个自然数,已随机放入一个有98个元素的数组a[98].要求写出一个尽量简单的方案找出没有被放入数组的那2个数,并在屏幕上打印这2个数
void test7() { try { ]; ]; ]; ; ; int i; ; i < num.Length; i++) { num[i] = i + ; num1[i] = i + ;/ ...
- ugui点击穿透判断
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Eve ...
- LSTM用于MNIST手写数字图片分类
按照惯例,先放代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 ...
- 应用安全 - 工具 - 浏览器 - IE浏览器 - 漏洞汇总
CVE-2014-6332 Date 2014.11 CVE-2016-0189 | MS16-051 Date 2016年初 CVE-2018-8174
- pkg-config too old的解决方法
linux下安装一些库时,会提示pkg-config too old,可以尝试下面的命令 apt-get install pkg-config
- CentOS7.查看进程占用端口情况
1.命令:"netstat -lntp" 2.没有改命令的话,需要安装 net-tools工具:"yum install net-tools" 3. 4. 5.
- docker安装tomcat&部署javaweb程序
一.docker定制简单的java-web应用镜像 网址: 1.jdk下载网址:https://www.oracle.com/technetwork/java/javase/downloads/jdk ...
- 2019CSP-S游记(?)
认识我的人都知道,我懒得写算法和模拟赛的博客,但是游记就不一样了,它比较好玩. Day0 中午随便收拾了下就坐高铁出发了,一个小时左右就到南昌了,随后坐公交,再步行到宾馆安置(也没多远). 宾馆离学校 ...
- Docker最详细入门教程
Docker原理.详细入门教程 https://blog.csdn.net/deng624796905/article/details/86493330 阮一峰Docker入门讲解 http://ww ...