力扣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/

相关推荐
财情双生教练易和中4 小时前
“找矿大会战”产业链,量化统计研究报告
职场和发展·职业发展·创业创新·程序员创富
不会就选b4 小时前
算法日常・每日刷题--<贪心>26
数据结构·算法
yuanxi2005 小时前
001267拟扩产光模块:大客户销售如何用价值力读一条扩产公告
人工智能·职场和发展·创业创新·学习方法
Interview Aid1125 小时前
Google SWE Intern Interview Process:OA、Coding 与面试流程详解
面试·职场和发展
hetao17338376 小时前
2026-09-29 hetao1733837 的刷题记录
c++·算法
欣欣之王来了7 小时前
AI合规专项:AI算法透明度的合规要求
人工智能·算法
wzdark7 小时前
分块思想在算法优化中的实践与应用4
算法
Because_of_Her17 小时前
dfs-洪水填充技巧
笔记·算法·深度优先·洪水填充
小静AI工程实验室7 小时前
RSA 算法详解:从模幂原理到 OAEP 加密、PSS 签名与 Python 实验
python·算法
xzal127 小时前
GESP Python笔记
笔记·python·算法