LeetCode1124. Longest Well-Performing Interval

这篇具有很好参考价值的文章主要介绍了LeetCode1124. 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].
Example 2:

Input: hours = [6,6,6]
Output: 0

Constraints:

1 <= hours.length <= 104
0 <= hours[i] <= 16文章来源地址https://www.toymoban.com/news/detail-811494.html

二、题解

class Solution {
public:
    int longestWPI(vector<int>& hours) {
        int n = hours.size();
        unordered_map<int,int> map;
        int res = 0,sum = 0;
        map[0] = -1;
        for(int i = 0;i < n;i++){
            hours[i] > 8 ? sum += 1 : sum += -1;
            if(sum > 0) res = i + 1;
            else{
                if(map.find(sum-1) != map.end()){
                    res = max(res,i-map[sum-1]);
                }
            }
            if(map.find(sum) == map.end()) map[sum] = i;
        }
        return res;
    }
};

到了这里,关于LeetCode1124. Longest Well-Performing Interval的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【递增栈】个人练习-Leetcode-845. Longest Mountain in Array

    题目链接:https://leetcode.cn/problems/longest-mountain-in-array/ 题目大意:给出一个数组 arr[] ,定义【山数组】为【长度大于等于3】【前面部分递增,后面部分递减,存在一个山峰元素】的数组。求 arr[] 中的最长的是【山数组】的子列。 思路:递增栈当然可以,不过刚好想到另一个

    2024年02月06日
    浏览(41)
  • LeetCode 1048. Longest String Chain【记忆化搜索,动态规划,哈希表,字符串】中等

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月05日
    浏览(33)
  • once do, do it well

    昨天出现一个生产问题。我们的channel系统代码里,调用其中一个三方服务商的http接口时未设置超时时间。碰巧昨天出现一笔http请求持续数小时始终无响应,加之程序是单线程处理交易请求,就出现因为线程一直处于RUNNABLE状态而导致系统生产能力严重下降。 现在说这个结论

    2023年04月11日
    浏览(66)
  • Decode user requirements to design well-architected applications

    So, one of my favorite writers and thought leaders in the industry, Mark Schwarz, who is an enterprise strategist here at ALS, in his book, he actually says that it is time for the wall between IT and business to come down. He says that old business models long ago pitted IT people against business. So he calls it \\\"pitting the nerds against the suits.\\\" And h

    2024年02月03日
    浏览(22)
  • 687. Longest Univalue Path

    687. Longest Univalue Path l,r别写错

    2024年01月19日
    浏览(35)
  • 题解:ABC320B - Longest Palindrome

    链接:Atcoder。 链接:洛谷。 算法难度:C。 思维难度:C。 调码难度:C。 综合评价:入门。 字符串处理。 通过双层循环分别枚举第一个字符和最后一个字符遍历每个子串,在分别判断是否为回文串,在所有是回文串的里面取长度最大值。 O(|s|2)。 字符串截取用substr函数。

    2024年02月07日
    浏览(33)
  • LCS2 - Longest Common Substring II

    洛谷LCS2 - Longest Common Substring II 题目大意 给你一些字符串,求它们的最长公共子串。 字符串个数不超过 10 10 10 ,每个字符串的长度不超过 100000 100000 100000 。 题解 可以先看看LCS - Longest Common Substring。 这题与上面那题类似,只不过要多一些操作。 首先,用第一个字符串建一个

    2024年02月12日
    浏览(22)
  • 解决 ERROR: An error occurred while performing the step: “Building kernel modules“. See /var/log/nv

    目录 解决 ERROR: An error occurred while performing the step: \\\"Building kernel modules\\\" 1. 查看日志文件 2. 检查依赖项 3. 更新内核版本 在进行 NVIDIA 驱动程序安装时,如果出现类似以下错误提示: 那么你可能需要进行以下步骤来解决该问题: 首先,我们需要查看 ​ ​/var/log/nvidia-installer.lo

    2024年02月03日
    浏览(45)
  • 最长递增子序列(Longest Increasing Subsequence)-C语言实现

    前言 最长递增子序列属于经典的动态规划问题,属于约束条件下求最大子集的问题。这里的约束条件意思是,子序列需要严格按照递增条件产生,在这个前提之下,我们要求到最长的子序列。这类问题的衍生问题有很多,其本质都是穷举最大化子集。 问题描述 问题其实非常

    2024年02月02日
    浏览(43)
  • Jenkins踩坑:Failed to connect to repository : Error performing git command: git ls-remote -h http://17

    Jenkins搭建好之后,使用Jenkins新建构建任务,在填写git地址时,一直报错 截图 期初我以为是凭据错误的原因,重复试了好久也没有解决问题,后来才发现,是由于我安装Jenkins的服务器,没有安装git的客户端,所以才无法链接git 安装git客户端命令: 安装完git客户端之后,Je

    2024年02月13日
    浏览(58)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包