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

相关推荐
aWty_1 小时前
实分析入门(11)--Cantor三分集
学习·数学·算法·实变函数
兰令水1 小时前
leecodecode【二叉树递归+对称】【2026.6.1打卡-java版本】
算法
地平线开发者9 小时前
profiler debug 工具用法与高一致性策略
算法·自动驾驶
编程大师哥9 小时前
匿名函数 lambda + 高阶函数
java·python·算法
我叫袁小陌10 小时前
算法解题思路指南
算法
地平线开发者10 小时前
Conv+BN+Add+ReLU 融合机制简介
算法·自动驾驶
yuanyuan2o210 小时前
模型预训练:Hugging Face Transformers 基础
算法·ai·语言模型·自然语言处理·nlp·深度优先
杨充10 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法
妄想出头的工业炼药师10 小时前
GS slam mono
算法·开源