首页 > 其他分享 >1124.longest well performing interval

1124.longest well performing interval

时间:2023-07-26 16:15:57浏览次数:38  
标签:interval int well performing 1124 stk hours prefix res

Description

1124. Longest Well-Performing Interval (Medium)

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 <= 10⁴
  • 0 <= hours[i] <= 16

Solution

Monotone stack

First, we set element greater than 8 to be 1, elements less than 8 to be -1. And we compute the prefix sum of the new array to get a prefix sum array prefix.

We need to find the maximum j - i that satisfies prefix[j] - prefix[i]. First, consider the left endpoint. If prefix[i1] < prefix[i2] and i1 <= i2, then we don't need to consider taking i2 as the left endpoint. So we can traverse prefix forward, and push idx to the stack if prefix[idx] < prefix[stk.top()];

Then, let's traverse prefix backwards. If prefix[j1] > prefix[stk.top()], update res = std::max(res, r - stk.top()), then pop the element in the top. If traverse prefix forward, we may omit res in the case that prefix[j] < prefix[stk.top()].

Hash table

  • if (prefix[i] > 0), the i days is well-performing interval, res = max(res, i);
  • if (prefix[i] <= 0), if key prefix[i] don't occured in hash table ump before, ump[prefix[i]] = i, otherwise we don't update ump[prefix[i]], since the corresponding value of the key in hash table is less, so the difference (the same as length of interval) is greater; Otherwise the maximum length of well-performing interval ended on the ith day is ump[prefix[i]] - ump[prefix[i] - 1](there must be key prefix[i] - 1 in hash table, or length is 0, which means there is not such interval), since there only are 1 and -1 in new array, the value prefix[i] - 1 must occur earlier than prefix[i] - 2 in array prefix.

Code

Monotone stack

class Solution {
  public:
    int longestWPI(vector<int> &hours) {
        int n = hours.size();
        if (n == 1) {
            return hours[0] > 8;
        }
        for (auto &i : hours) {
            if (i > 8) {
                i = 1;
            } else {
                i = -1;
            }
        }
        // 计算新hours的前缀和
        vector<int> prefix(n + 1, 0);
        for (int i = 1; i <= n; i++) {
            prefix[i] = prefix[i - 1] + hours[i - 1];
        }
        //
        stack<int> l_stk;
        int res = 0;
        l_stk.push(0);
        for (int i = 1; i <= n; i++) {
            if (prefix[i] > 0) {
                res = std::max(res, i);
            }
            if (prefix[i] < prefix[l_stk.top()]) {
                l_stk.push(i);
            }
        }
        for (int r = n; r >= 1; r--) {
            while (!l_stk.empty() && prefix[r] > prefix[l_stk.top()]) {
                if (l_stk.empty()) {
                    return std::max(r, res);
                }
                res = std::max(res, r - l_stk.top());
                l_stk.pop();
            }
        }
        return res;
    }
};

Hash table

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

标签:interval,int,well,performing,1124,stk,hours,prefix,res
From: https://www.cnblogs.com/zwyyy456/p/17582720.html

相关文章

  • 使用maxwell实现数据库主从同步
    前置条件maxwell使用1.29.2版本,再高的版本不支持JDK1.8。使用Maxwell之前需要准备搭建以下环境 在https://www.cnblogs.com/szhNJUPT/p/17574193.html有详细搭建过程mysql采用5.7.43,尝试过mysql8.0版本,但是由于utf8mb3字符集在mysql8.0版本被舍弃,导致maxwell连接失败。数据......
  • maxwell方程组
    Maxwell方程组是一组描述电场、磁场与电荷密度和电流密度之间关系的偏微分方程,其偏微分形式如下:式中,E为电场强度;B为磁感应强度;D为电位移矢量;H为磁场强度。maxwell方程组积分形式:(1)静电场高斯定理该方程描述了电荷如何产生电场,电场强度对任意封闭曲面的通量只取决于该封闭曲面......
  • Flutter | 使用 InkResponse和 InkWell组件 实现事件操作
    可以包裹不具备事件处理的组件,实现水波纹等点击事件的效果;InkWell水波纹限制在文本组件之内;InkResponse水波纹没有限制;InkResponse和InkWell都可以指定各种响应颜色、手势等相关属性;  InkWell(radius:200.0,focusColor:Colors.red,hove......
  • 通过Maxwell同步mysql数据至kafka
    实验环境本地虚拟机maraidb10.8.8kafka2.12-3.3.1maxwell由容器部署1mariadb1.1配置log_bin配置文件中加入如下内容server-id=111log_bin=mysql-binbinlog_format=ROWexpire_logs_days=1重启服务systemctlrestartmariadb查询命令SHOWVARIABLESLI......
  • Facebook 最新可佩戴 AR 设备、AR 设备未来五年市场扩张、语音社交新创Swell等|Decode
     DecodetheWeek ≠音视频技术周刊 Credit: MeKyeoungLee /NewsBriefing. Clubhouse 聘请 Instagram前高管 Clubhouse聘请曾任职Instagram的FadiaKader担任新的媒体合作和创作主管。此前,OWN和Netflix的前高层MayaWatson被CH聘请担任其全球营销主管,这表明......
  • MAXWELL两线圈静磁场仿真
    【1】初始界面 【2】选中MAXWELL3D仿真 默认为静磁场,刚好是需要的,无需改动  【3】放置原边线圈 主要需要修改的参数 PolygonSegments:Numberofcross_sectionsegments,0forcircle。截面多边形线段数,0表示圆。PolygonRadius:outerradiusofcross-sectonpoly......
  • Maxwell仿真线圈自感互感1
    【1】新建文件   【2】设置涡流场新建文件后页面默认为:magnetostatic静磁场,需要修改为EddyCurrent涡流场 相关回答:maxwell中涡流场和瞬态场的区别是什么:涡流场和瞬态场都可以求解,推荐涡流场,精度高【3】利用Draw中的软件内置UDP(UserDefinedPrimitives)或自定......
  • HONEYWELL工业模块SPS5713 51199930-100
    W;① ⑧ 0 ③ 0 ① 7  7  ⑦ 5 ⑨HONEYWELL工业模块SPS571351199930-100,05074-A-012205704-A-012105704-A-0131,05701-A-0361,05704-A-0146,05704-A-0145,05701-A-0361,05704-A-0144。SC-PCMX0151307195-175,05701-A-0550,一般信息产品编号:SYN5201-2277......
  • 适定问题(Well-posed problem)与不适定问题(ill posed problem)
    Well-posedproblem&Ill-posedproblem.适定问题(Well-posedproblem)是指满足下列三个要求的问题:asolutionexists:解必须存在;thesolutionisunique:解必须唯一;thesolution’sbehaviorchangescontinuouslywiththeinitialconditions:解能根据初始条件连续变化,不会发......
  • 数据库流转工具—Maxwell
    第1章Maxwell简介1.1Maxwell概述Maxwell是由美国Zendesk公司开源,用Java编写的MySQL变更数据抓取软件。它会实时监控Mysql数据库的数据变更操作(包括insert、update、delete),并将变更数据以JSON格式发送给Kafka、Kinesi等流数据处理平台。官网地址:http://maxwells-daemon.io/......