本题题意:

在数组中,找到最大的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. 签名时出错: 未能对** SignTool Error:

    项目在vs2010创建,在2017上运行时报签名时出错.......... 解决方法: 右键项目 - 属性-签名 -  创建测试证书 - 密码可以为空-确定

  2. 使用NDK(r20)编译FFmpeg

    前两天在论坛上看到一个问题,大意是怎么在UBUNTU下使用NDK-r20编译FFmpeg.我第一反应是不该用r20,因为我在很早前用过没有gcc版本的NDK,发现有很多问题不能编译,就立马回复了个使用 ...

  3. Linux shell--基础指令

    Linux shell--基础指令 浏览Linux文件系统 Linux中最基础也是最必要的一条指令 cd destination cd命令可接受单个参数destination,用以指定想切换到的目录名 ...

  4. MyBatis与log4j

    1.前言   在项目中编写Sysem.out.prinltn()的时候,是输出到控制台的,当项目发布到tomcat之后,是没有控制台的,不过可以在命令行界面还能看见,但是不容易观察一些输出结果.log ...

  5. tomcat修改进程名称

    1.window平台: 打开tomcat_home\bin\setclasspath.bat文件,找到set _RUNJAVA=”%JRE_HOME%\bin\java”这一行. 将该行注释掉 ,然后 ...

  6. CSS绘制三角形和箭头,不用再用图片了

    前言 还在用图片制作箭头,三角形,那就太lou了.css可以轻松搞定这一切,而且颜色大小想怎么变就怎么变,还不用担心失真等问题. 先来看看这段代码: /**css*/.d1{ width: 0; he ...

  7. linux内核的冷热页分配器

    先说说cpu的cache,和cpu的cache比起来访问主内存是非常慢的,为了加快速度根据本地性原则,cpu在访问主内存的时候会把附近的一块数据都加载到cpu的cache里,之后读写这块数据都是在ca ...

  8. jmeter使用问题——数据库无法连接

    使用Jmeter 数据库连接配置组件,执行sql时jmeter日志报错 WARN o.a.j.p.j.p.AbstractJDBCProcessor: SQL Problem in 查询账号申诉ID: ...

  9. 201871010113-刘兴瑞《面向对象程序设计(java)》第八周学习总结

    项目 内容 这个作业属于哪个课程 <任课教师博客主页链接> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 <作业链接地址>htt ...

  10. poj 1852 ants 题解《挑战程序设计竞赛》

    地址  http://poj.org/problem?id=1852 题目描述 Description An army of ants walk on a horizontal pole of len ...