【单调栈】LeetCode2334:元素值大于变化阈值的子数组

简介: 【单调栈】LeetCode2334:元素值大于变化阈值的子数组

题目

给你一个整数数组 nums 和一个整数 threshold 。

找到长度为 k 的 nums 子数组,满足数组中 每个 元素都 大于 threshold / k 。

请你返回满足要求的 任意 子数组的 大小 。如果没有这样的子数组,返回 -1 。

子数组 是数组中一段连续非空的元素序列。

示例 1:

输入:nums = [1,3,4,3,1], threshold = 6

输出:3

解释:子数组 [3,4,3] 大小为 3 ,每个元素都大于 6 / 3 = 2 。

注意这是唯一合法的子数组。

示例 2:

输入:nums = [6,5,6,5,8], threshold = 7

输出:1

解释:子数组 [8] 大小为 1 ,且 8 > 7 / 1 = 7 。所以返回 1 。

注意子数组 [6,5] 大小为 2 ,每个元素都大于 7 / 2 = 3.5 。

类似的,子数组 [6,5,6] ,[6,5,6,5] ,[6,5,6,5,8] 都是符合条件的子数组。

所以返回 2, 3, 4 和 5 都可以。

提示:

1 <= nums.length <= 105

1 <= nums[i], threshold <= 109

枚举子数组的最小值

以nums[i]为最小值的左边界是从右向左第一个小于nums[i]的数,假定其下标为left。如果不存在,left为-1。

以nums[i]为最小值的右边界是从左向右第一个小于等于nums[i]的数,假定其下标为right。如果不存在,right为m_c。

则nums(left,right)是以nums[i]为最小值的最长子数组。显然,相同最小值,子数组越长越容易符合题目。只需验证最长子数组。

代码

核心代码

class Solution {
public:
  int validSubarraySize(vector<int>& nums, int threshold) {
    m_c = nums.size();
    vector<int> vLeft(m_c, -1), vRight(m_c, m_c);//vLeft[i] 从右向左第一个小于nums[i] ;vRight[i] 是第一个小于等于nums[i]。
    stack<int> sta;
    for (int i = 0; i < m_c; i++)
    {
      while (sta.size() && (nums[sta.top()] >= nums[i]))
      {
        vRight[sta.top()] = i;
        sta.pop();
      }
      if (sta.size())
      {
        vLeft[i] = sta.top();
      }
      sta.emplace(i);
    }
    for (int i = 0; i < m_c; i++)
    {
      const int k = vRight[i] - vLeft[i] - 1;
      if ((k > 0) && (nums[i] > threshold / k))
      {
        return k;
      }
    }
    return -1;
  }
  int m_c;
};

测试用例

template<class T>
void Assert(const T& t1, const T& t2)
{
  assert(t1 == t2);
}
template<class T>
void Assert(const vector<T>& v1, const vector<T>& v2)
{
  if (v1.size() != v2.size())
  {
    assert(false);
    return;
  }
  for (int i = 0; i < v1.size(); i++)
  {
    Assert(v1[i], v2[i]);
  }
}
int main()
{
  vector<int> nums;
  int threshold;
  {
    Solution slu;
    nums = { 1, 3, 4, 3, 1 }, threshold = 6;
    auto res = slu.validSubarraySize(nums, threshold);
    Assert(3, res);
  }
  {
    Solution slu;
    nums = { 6,5,6,5,8 }, threshold = 7;
    auto res = slu.validSubarraySize(nums, threshold);
    //Assert(1, res);
  }
  //CConsole::Out(res);
}

2023年3月版

bool Less(const std::pair& p, int iData)
{
return p.first < iData;
}
class Solution {
public:
int validSubarraySize(vector& nums, int threshold) {
m_c = nums.size();
vector vLeft(m_c), vRight(m_c);
{
vector> vStack;
for (int i = 0; i < m_c; i++)
{
int iNum = std::lower_bound(vStack.begin(), vStack.end(), nums[i], Less) - vStack.begin();
if (0 == iNum)
{
vLeft[i] = -1;
}
else
{
vLeft[i] = vStack[iNum - 1].second;
}
while (vStack.size() && (nums[i] <= vStack.back().first))
{
vStack.pop_back();
}
vStack.emplace_back(nums[i], i);
}
}
{
vector> vStack;
for (int i = m_c - 1; i >= 0;i–)
{
int iNum = std::lower_bound(vStack.begin(), vStack.end(), nums[i]+1, Less) - vStack.begin();
if (0 == iNum)
{
vRight[i] = m_c;
}
else
{
vRight[i] = vStack[iNum - 1].second;
}
while (vStack.size() && (nums[i] <= vStack.back().first))
{
vStack.pop_back();
}
vStack.emplace_back(nums[i], i);
}
}
for (int i = 0; i < m_c; i++)
{
const int len = vRight[i] - vLeft[i] - 1;
if (nums[i] > threshold / len)
{
return len;
}
}
return -1;
}
int m_c;
};

2023年7月版

class Solution {
public:
int validSubarraySize(vector& nums, int threshold) {
m_c = nums.size();
vector vLeft(m_c), vRight(m_c);
{//计算左边的长度
stack> staValueIndex;
for (int i = 0; i < m_c; i++)
{
while (staValueIndex.size() && (staValueIndex.top().first >= nums[i]))
{
staValueIndex.pop();
}
vLeft[i] = staValueIndex.empty() ? i : (i - staValueIndex.top().second - 1);
staValueIndex.emplace(nums[i], i);
}
}
{//计算右边的长度
stack> staValueIndex;
for (int i = m_c-1 ; i >= 0 ; i-- )
{
while (staValueIndex.size() && (staValueIndex.top().first >= nums[i]))
{
staValueIndex.pop();
}
vRight[i] = staValueIndex.empty() ? m_c-1-i : ( staValueIndex.top().second - i - 1);
staValueIndex.emplace(nums[i], i);
}
}
int iRet = -1;
for (int i = 0; i < m_c; i++)
{
const int len = vLeft[i] + vRight[i]+1;
const int iNeedK = threshold / nums[i] + 1;
if (len >= iNeedK)
    {
      iRet = max(iRet, len);
    }
  }
  return iRet;
}
int m_c;

};


扩展阅读

视频课程

有效学习:明确的目标 及时的反馈 拉伸区(难度合适),可以先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。

https://edu.csdn.net/course/detail/38771

如何你想快

速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程

https://edu.csdn.net/lecturer/6176

相关

下载

想高屋建瓴的学习算法,请下载《喜缺全书算法册》doc版

https://download.csdn.net/download/he_zhidan/88348653

测试环境

操作系统:win7 开发环境: VS2019 C++17

或者 操作系统:win10 开发环境: VS2022 C++17

如无特殊说明,本算法C++ 实现。



相关文章
|
2月前
|
存储 算法 测试技术
力扣经典150题第五十四题:最小栈
力扣经典150题第五十四题:最小栈
30 0
|
3月前
|
存储 算法 索引
力扣每日一题 6/24 模拟 数组 单调栈
力扣每日一题 6/24 模拟 数组 单调栈
27 0
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
18 4
|
1月前
|
Python
【Leetcode刷题Python】946. 验证栈序列
LeetCode题目“946. 验证栈序列”的Python解决方案,通过模拟栈的压入和弹出操作来验证给定的两个序列是否能通过合法的栈操作得到。
21 6
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 09. 用两个栈实现队列
使用两个栈实现队列的Python解决方案,包括初始化两个栈、实现在队列尾部添加整数的appendTail方法和在队列头部删除整数的deleteHead方法,以及相应的示例操作。
33 2
|
1月前
|
算法 Python
【Leetcode刷题Python】子数组查找
一个用于寻找给定字符串中最长重复子串的Python函数实现,采用了滑动窗口的方法来检测重复的子串。
15 1
|
1月前
|
算法 Java
LeetCode初级算法题:子数组最大平均数+二叉树的最小深度+最长连续递增序列+柠檬水找零
LeetCode初级算法题:子数组最大平均数+二叉树的最小深度+最长连续递增序列+柠檬水找零
33 0
|
1月前
|
Python
【Leetcode刷题Python】232. 用栈实现队列
如何使用Python语言通过两个栈来实现队列的所有基本操作,包括入队(push)、出队(pop)、查看队首元素(peek)和判断队列是否为空(empty),并提供了相应的代码实现。
15 0
|
2月前
|
Python
155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题
155. 最小栈 力扣 python 空间换时间 o(1) 腾讯面试题
|
2月前
|
存储 算法
经典的滑动窗口的题目 力扣 2799. 统计完全子数组的数目(面试题)
经典的滑动窗口的题目 力扣 2799. 统计完全子数组的数目(面试题)