962. Maximum Width Ramp
本题题意:
在数组中,找到最大的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的更多相关文章
- 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 ...
- 【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]. ...
- 【LeetCode】962. Maximum Width Ramp 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址:https://leetco ...
- [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 ...
- 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 ...
- 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 ...
- 单调栈-Maximum Width Ramp
2020-01-23 19:39:26 问题描述: 问题求解: public int maxWidthRamp(int[] A) { Stack<Integer> stack = new ...
- [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 ...
- [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 ...
随机推荐
- Git - Git简介与客户端安装
简介 Git是目前世界上最先进的分布式版本控制系统(没有之一)! 集中式版本控制系统(CVS/SVN),版本库是集中存放在中央服务器的,而一般工作的时候,用的都是自己的电脑,所以要先从中央服务器取得最 ...
- ASP.NET MVC Action向视图传值之匿名类型
在使用ASP.NET MVC过程中想必大家都有遇到过一个问题就是我们的Action如何向视图传递匿名类型的值呢,如果不做特殊处理则无法实现. 接下来我们来看一个示例: 在我们的控制中: using S ...
- git push 时用户的配置
Pycharm临时配置git提交的账户:git 修改当前的project的用户名的命令为:git config user.name 你的目标用户名**;git 修改当前的project提交邮箱的命令为 ...
- Prism_Commanding(2)
Commanding 除了提供对要在视图中显示或编辑的数据的访问之外,ViewModel还可能定义可由用户执行的一个或多个动作或操作.用户可以通过UI执行的动作或操作通常被定义为命令.命令提供了一种方 ...
- 20180918 begin
20180918-20190717 风 雅 颂(305,每天一首): 诗经鉴赏, 180918-1030 魔鬼经济学 <唐宋词十七讲>叶嘉莹<最美的宋词> 布谷鸟<诗境浅 ...
- 三、ITK的dcm图像读写
一.主要功能 1.读取单张dcm图像 2.写入单张dcm图像 3.图像调整之后以.jpg格式写入 4.调整之后重新以.dcm格式写入 二.代码 #include "itkImageFileR ...
- C# 常用排序算法
文章引用地址:https://www.cnblogs.com/fengyeqingxiang/archive/2019/06/14/11021852.html C#所有经典排序算法汇总 1 2 3 ...
- 0day2安全——笔记1
第一章 PE和内存之间的映射 节偏移 文件偏移地址(File Offset Address):数据在PE文件中的地址 装载地址(Image Base):PE装入内存的基地址 虚拟内存地址(Virtua ...
- Noip2017Day1T3 逛公园
题目链接 problem 一个有向无重边自环图,设\(D\)为从\(1\)号点走到\(n\)号点的最短距离.问有多少条从\(1\)到\(n\)的路径长度不超过\(D+K\).\(K\)为给定的值,且\ ...
- Vue之外的杂谈笔记
1.老项目的构建输出为什么臃肿? 引用:(引自http://www.cnblogs.com/linfangshuhellowored/p/5657285.html) 答:因为使用的是require的r ...