首页 > 其他分享 > [leetcode每日一题]2.14

[leetcode每日一题]2.14

时间:2023-02-14 18:06:40浏览次数:54  
标签:int res 每日 元素 Solution stk hours 2.14 leetcode

​1124. 表现良好的最长时间段​

难度中等253

给你一份工作时间表 ​​hours​​,上面记录着某一位员工每天的工作小时数。

我们认为当员工一天中的工作小时数大于 ​​8​​ 小时的时候,那么这一天就是「劳累的一天」。

所谓「表现良好的时间段」,意味在这段时间内,「劳累的天数」是严格 大于「不劳累的天数」。

请你返回「表现良好时间段」的最大长度。

 

示例 1:

输入:hours = [9,9,6,0,6,6,9]
输出:3
解释:最长的表现良好时间段是 [9,9,6]。

示例 2:

输入:hours = [6,6,6]
输出:0

提示:

  • ​1 <= hours.length <= 104
  • ​0 <= hours[i] <= 16​

Solution

前缀和两维遍历。勉强能过。

class Solution {
public:
int longestWPI(vector<int>& hours) {
int n = hours.size();
int prefix[10005] = {0};
for(int i=0;i<n;i++){
prefix[i+1] += prefix[i] + (hours[i] > 8 ? 1 : -1);
}
int res = 0;
for(int i=1;i<=n;i++){
for(int j=0;j<i;j++){
if(prefix[j] < prefix[i]){
res = max(res, i - j);
break;
}
}
}
return res;
}
};

优化:可以使用哈希表记录每一个数据第一次出现的下标。

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

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/longest-well-performing-interval/solution/biao-xian-liang-hao-de-zui-chang-shi-jia-rlij/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

优化2:可以使用栈。

正序遍历,每次入栈的元素都要比当前栈顶元素小。这样确保了每一次的访问都是下标第一小的元素,即区间左端点最小。

之后倒序遍历前缀和数组,用当前元素匹配栈顶元素。如果当前元素比栈顶元素大,那么就合法,弹出栈顶并更新当前答案,直到栈顶元素比当前元素小,则前缀和数组遍历下一个元素。这样能够保证区间右端点最大。

class Solution {
public:
int longestWPI(vector<int>& hours) {
int n = hours.size();
vector<int> s(n + 1);
stack<int> stk;
stk.push(0);
for (int i = 1; i <= n; i++) {
s[i] = s[i - 1] + (hours[i - 1] > 8 ? 1 : -1);
if (s[stk.top()] > s[i]) {
stk.push(i);
}
}

int res = 0;
for (int r = n; r >= 1; r--) {
while (stk.size() && s[stk.top()] < s[r]) {
res = max(res, r - stk.top());
stk.pop();
}
}
return res;
}
};

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/longest-well-performing-interval/solution/biao-xian-liang-hao-de-zui-chang-shi-jia-rlij/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

很巧妙地一道题。不过充满了资本家的恶趣味。

标签:int,res,每日,元素,Solution,stk,hours,2.14,leetcode
From: https://blog.51cto.com/u_15763108/6057341

相关文章

  • LeetCode03 无重复字符的最长子串
    暴力解法,O(n²)publicintlengthOfLongestSubstring(Strings){ArrayList<Integer>lenList=newArrayList<>();for(inti=0;i<s.length......
  • 2022.2.14 每日十题
    一个组织正在采用敏捷的思维方式。在第一个敏捷项目中,项目经理面临一个问题,因为团队不能及时做出决定。项目经理应该怎么做来解决这个问题?A.为新的组织政策下的决策制定......
  • 每日一题.截断数组
    先特判,显而易见数组的前缀和必须是3的倍数,要不然分不成三份。然后就是遍历前缀和让它和1/3总和和2/3总和比,显然当第二个1/3也成立的时候就可以停止遍历,然后可以继续遍历或......
  • [LeetCode] 1330. Reverse Subarray To Maximize Array Value 翻转子数组得到最大的数
    Youaregivenanintegerarray nums.The value ofthisarrayisdefinedasthesumof |nums[i]-nums[i+1]| forall 0<=i<nums.length-1.Youare......
  • 每日一个 CF hack 小寄巧
    \(\text{rt}\),贺的CFhack区一位老哥的。#include<bits/stdc++.h>#defineall(x)(x).begin(),(x).end()usingnamespacestd;typedefunsignedlonglongull;ty......
  • 代码随想录算法训练营第二十四天|LeetCode 77. 组合
    77.组合文章:代码随想录(programmercarl.com)视频:带你学透回溯算法-组合问题(对应力扣题目:77.组合)|回溯法精讲!_哔哩哔哩_bilibili思路:那么我把组合问题抽象为如下树形......
  • leetcode:合并2个有序链表-easy
    题目:将两个升序链表合并为一个新的升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:l1=[1,2,4],l2=[1,3,4]输出:[1,1,2,3,4,......
  • leetcode:求两数之和-easy
    题目:给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target 的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答......
  • 【LeeCode】739. 每日温度
    【题目描述】给定一个整数数组 ​​temperatures​​ ,表示每天的温度,返回一个数组 ​​answer​​ ,其中 ​​answer[i]​​ ​​i​​ 天,下一个更高温度出现在几天后......
  • 【栈】LeetCode 394. 字符串解码
    题目链接394.字符串解码思路建立一个数字栈numStack和一个字符串栈stringBuilderStack。遍历字符串s:遇到数字和字符时按照相应规则分别累加进k和result中。......