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

相关推荐
邵奈一几秒前
当沉香之乡遇上 Seed-2.1-pro:我用 AI 给村子做了一个数字门面
算法·架构
海天一色y9 分钟前
图像分割全解析:从经典算法到深度学习的原理与实战(Python + MATLAB)
python·深度学习·算法
垆边人似月.28 分钟前
华为算法题:最长连续递增子序列的变
数据结构·算法·leetcode
YOLO数据集集合29 分钟前
机器学习二分类预测算法组合工具:12种算法、113种组合的批量建模方案8031期
算法·机器学习
longlongzihan40 分钟前
LeetCode 56合并区间:排序与贪心
c++·算法·leetcode
顶点多余1 小时前
9.24 面试复盘
面试·职场和发展
学术小李1 小时前
cudaMalloc第一个参数为何是指针的指针
算法
kukubuzai1 小时前
二分查找系列一
数据结构·算法·leetcode
老歌老听老掉牙1 小时前
动态规划:从递归到最优决策的算法艺术
算法·动态规划