力扣962.最大宽度坡

力扣962.最大宽度坡

  • 单调栈

    • 栈中存从nums[0]开始的一个单调下降 的序列
      • 为了让坡的底尽量小
    • 然后倒序遍历nums,找符合条件的栈中元素 弹出
cpp 复制代码
  class Solution {
  public:
      int maxWidthRamp(vector<int>& nums) {
          int n = nums.size();
          int res = 0;
          stack<int> st;
          for(int i=0;i<n;i++)
              if(st.empty() || nums[st.top()] > nums[i])
                  st.emplace(i);
          for(int i=n-1;i>=0;i--)
          {
              while(!st.empty() && nums[st.top()] <= nums[i])
              {
                  int t = st.top();
                  st.pop();
                  res = max(res,i - t);
              }
          }
          return res;
      }
  };

参考题解:https://leetcode.cn/problems/maximum-width-ramp/solutions/666604/zui-da-kuan-du-po-dan-diao-zhan-cun-de-s-myj9/

相关推荐
面试鸭1 天前
携程开启秋招补录
职场和发展·互联网
涛涛北京1 天前
【强化学习实验】- 策略梯度算法
人工智能·算法
栀秋6661 天前
深入浅出链表操作:从Dummy节点到快慢指针的实战精要
前端·javascript·算法
Pyeako1 天前
机器学习之KNN算法
人工智能·算法·机器学习
xhxxx1 天前
从被追问到被点赞:我靠“哨兵+快慢指针”展示了面试官真正想看的代码思维
javascript·算法·面试
可信计算1 天前
【算法随想】一种基于“视觉表征图”拓扑变化的NLP序列预测新范式
人工智能·笔记·python·算法·自然语言处理
月明长歌1 天前
【码道初阶】【LeetCode 110】平衡二叉树:如何用一个“Magic Number”将复杂度从O(N²)降为 O(N)?
linux·算法·leetcode
yaoh.wang1 天前
力扣(LeetCode) 14: 最长公共前缀 - 解法思路
python·程序人生·算法·leetcode·面试·职场和发展·跳槽
历程里程碑1 天前
C++ 9 stack_queue:数据结构的核心奥秘
java·开发语言·数据结构·c++·windows·笔记·算法
t198751281 天前
基于MATLAB的线性判别分析(LDA)降维算法实现方案
开发语言·算法·matlab