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

相关推荐
天赐学c语言1 小时前
1.6 - 复制IP地址 && vector和list的区别
c++·算法·leecode
多米Domi0111 小时前
0x3f 第23天 黑马web (前端三件套,maven,web入门、mysql)黑马反射注解 hot100普通数组
java·python·mysql·算法·leetcode·maven
上海锟联科技1 小时前
DAS-U1000 极致版解调卡
数据结构·算法·嵌入式实时数据库
Swift社区10 小时前
LeetCode 465 最优账单平衡
算法·leetcode·职场和发展
聆风吟º10 小时前
【数据结构手札】空间复杂度详解:概念 | 习题
java·数据结构·算法
weixin_4450547210 小时前
力扣热题51
c++·python·算法·leetcode
地平线开发者11 小时前
linux 常见稳定性问题分析方法
算法·自动驾驶
s砚山s11 小时前
代码随想录刷题——二叉树篇(九)
算法
地平线开发者11 小时前
大模型常见量化方法简介
算法·自动驾驶
smj2302_7968265213 小时前
解决leetcode第3801题合并有序列表的最小成本
数据结构·python·算法·leetcode