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

相关推荐
Frostnova丶3 小时前
【算法笔记】数学知识
笔记·算法
吴可可1234 小时前
AutoCAD 2016与2014二次开发关键差异
算法
雨白5 小时前
哈希:以时间换空间的算法实战
算法
San813_LDD6 小时前
[数据结构]LeetCode学习
数据结构·算法·图论
x138702859577 小时前
c语言排雷游戏(基础版9*9)
c语言·算法·游戏
sheeta19988 小时前
LeetCode 每日一题笔记 日期:2026.06.06 题目:2196. 根据描述创建二叉树
笔记·算法·leetcode
小欣加油8 小时前
leetcode994 腐烂的橘子
数据结构·c++·算法·leetcode·bfs
QuZero8 小时前
Guava Cache Deep Dive
java·后端·算法·guava
随意起个昵称9 小时前
线性dp-LIS题目4(A Twisty Movement)
算法·动态规划