力扣962.最大宽度坡

力扣962.最大宽度坡

  • 单调栈

    • 栈中存从nums0开始的一个单调下降 的序列
      • 为了让坡的底尽量小
    • 然后倒序遍历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/

相关推荐
罗西的思考6 小时前
【Agentic RL】Dressage Staleness 控制深度分析
人工智能·算法·机器学习
quaer7 小时前
弗洛伊德算法--判圈法寻找环的入口O(n) O(1)
数据结构·算法
文丑颜不良啊8 小时前
LeetCode51~75题目解析
leetcode
高晶8 小时前
无乘法器实现LC二阶滤波器:基于移位运算的快速算法与四种精度推导
算法·设计模式·程序员
ysbfsy8 小时前
大数计算思路
算法
高晶8 小时前
主从自动分配算法:平均值乘以设备数一半的选举基准值解析
算法·设计模式·程序员
学电子她就能回来吗9 小时前
LeetCode Hot 100|普通数组专题学习笔记(零基础版)
笔记·算法·leetcode
用户老李9 小时前
反向传播计算梯度举例
算法
niyongsheng9 小时前
多智能体连续时间内路径规划:AA-SIPP(m) 的 Rust 实现与算法
算法·rust·机器人
招聘背调实务专家杨宏9 小时前
上级评价出现主观偏差与口径冲突时如何校准
职场和发展