本题题意:

在数组中,找到最大的j-i,使得i<j and A[i] <= A[j]

思路:

维持一个递减的栈,遇到比栈顶小的元素,进栈;

比大于等于栈顶的元素-> 找到栈中第一个小于等于该元素的下标

时间:O(Nlgn),空间:O(N)

List<Integer> s = new ArrayList<>();
int res = 0;
for (int i = 0; i < A.length; i++){
if (s.isEmpty() || A[ s.get(s.size() - 1) ] > A[i]){
s.add(i);
}else{
int left = 0, right = s.size() - 1;
int mid = 0;
while (left < right){
mid = (left + right) / 2;
if (A[s.get(mid)] > A[i]){
left = mid + 1;
}else{
right = mid;
}
}
res = Math.max(res, i - s.get(left));
}
}

进一步的栈利用:

    Stack<Integer> s = new Stack<>();
int res = 0;
for (int i = 0; i < A.length;i++){
if (s.isEmpty() || A[s.peek()] > A[i]){
s.add(i);
}
}
for (int i = A.length - 1; i > res; i--){
while (!s.isEmpty() && A[s.peek()] <= A[i]){
res = Math.max(res, i - s.pop());
}
}
return res;

962. Maximum Width Ramp的更多相关文章

  1. LC 962. Maximum Width Ramp

    Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j].  The ...

  2. 【leetcode】962. Maximum Width Ramp

    题目如下: Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j]. ...

  3. 【LeetCode】962. Maximum Width Ramp 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址:https://leetco ...

  4. [Swift]LeetCode962. 最大宽度坡 | Maximum Width Ramp

    Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j].  The ...

  5. Maximum Width Ramp LT962

    Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j].  The ...

  6. 116th LeetCode Weekly Contest Maximum Width Ramp

    Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j].  The ...

  7. 单调栈-Maximum Width Ramp

    2020-01-23 19:39:26 问题描述: 问题求解: public int maxWidthRamp(int[] A) { Stack<Integer> stack = new ...

  8. [LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  9. [Swift]LeetCode662. 二叉树最大宽度 | Maximum Width of Binary Tree

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

随机推荐

  1. C# IE环境 - 重置IE( 注册表)

    IE设置,都可以通过注册表,修改.以下是一些常用的IE设置注册表修改~ 检查证书吊销 /// <summary> /// 检查证书是否吊销 /// </summary> /// ...

  2. 【C#】学习笔记 Linq相关

    Language-Integrated Query(语言集成查询) 写了个demo,具体看

  3. SSH的本地、远程、动态端口转发实验笔记

    SSH端口转发 SSH 会自动加密和解密所有 SSH 客户端与服务端之间的网络数据.但是,SSH 还能够将其他 TCP 端口的网络数据通过 SSH 链接来转发,并且自动提供了相应的加密及解密服务.这一 ...

  4. 【转载】XSS攻击和sql注入

    XSS攻击: https://www.cnblogs.com/dolphinX/p/3391351.html 跨站脚本攻击(Cross Site Script为了区别于CSS简称为XSS)指的是恶意攻 ...

  5. [数组] Leetcode 189.旋转数组

  6. (转)玩转Koa -- koa-bodyparser原理解析

    地址:http://www.imooc.com/article/274059 一.前置知识   在理解koa-bodyparser原理之前,首先需要了解部分HTTP相关的知识. 1.报文主体   HT ...

  7. ajax给全局变量设置值,请先关掉异步上传效果

    $.ajax({ type: 'POST', url: "/downloadExcelInfo", timeout: 0, async: false, contentType: & ...

  8. Leetcode103_Binary-tree-zigzag-level-order-traversal

    原题: https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/ 思路,如果无从下手,先要了解二叉树的广度优 ...

  9. for循环创建的a标签,当点击时如何确定点击的是哪一个标签?

    创建 代码: js: 效果: 原因: html代码:这种获取选中标签的方式,是通过监听body来实现的,所以body上要增加这个onclick(this)

  10. Please make sure you have the correct access rights and the repository exists.

    参考:https://blog.csdn.net/jingtingfengguo/article/details/51892864,感谢老哥. 从码云克隆项目到新的服务器上,报错: Please ma ...