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 ...
随机推荐
- 签名时出错: 未能对** SignTool Error:
项目在vs2010创建,在2017上运行时报签名时出错.......... 解决方法: 右键项目 - 属性-签名 - 创建测试证书 - 密码可以为空-确定
- selenium设置谷歌无头浏览器
from selenium import webdriver from selenium.webdriver.chrome.options import Options def Browse(): c ...
- java基础(3):变量、运算符
1. 变量 1.1 变量概述 前面我们已经学习了常量,接下来我们要学习变量.在Java中变量的应用比常量的应用要多很多.所以变量也是尤为重要的知识点! 什么是变量?变量是一个内存中的小盒子(小容器), ...
- SimpleDateFormat类简单学习
一.简介 SimpleDateFormat是一个格式化和解析日期的具体类,其可以将时间转化为指定格式的日期字符串,也可以将具有格式的日期字符串转换为时间. formatting (date → tex ...
- MySql Navicat可视化工具
下载链接 链接:https://pan.baidu.com/s/1ca5KbpCFc4UbcYkXZDu6aA 提取码:8nku 安装比较简单,选完安装路径,下一步即可 Navicat for MyS ...
- .net实现一个简单的通用查询数据、导出Excel的网页
背景:临时提供一个简单的网页,供其他人浏览数据库(Oracel.MSSQL)的某些数据,并导出Excel.支持在配置文件中随时添加或修改sql. 实现:把sql语句等信息保存一个xml文件中,前端页面 ...
- [20191113]oracle共享连接模式端口2.txt
[20191113]oracle共享连接模式端口2.txt --//昨天的测试链接:http://blog.itpub.net/267265/viewspace-2663651/=>[20191 ...
- 设置API:wx.openSetting,wx.getSetting使用说明(示例:地图授权与取消授权后的重新授权)
这个API解决了过去一个长久以来无法解决的问题,如何让用户重复授权: 打开小程序的设置界面:就是主动调取授权 目前资料极少,但是已经可以让大家先看看了: 官方文档地址:https://mp.weixi ...
- Linux系统中的截图功能(类似QQ、微信、Snipaste截图功能)
作者亲笔测试Ubuntu16.04,18.04,deepin15.11桌面版本Linux内核系统. 安装: 1. 终端命令黑框 2. sudo apt-get install flameshot(体积 ...
- 设计模式-State(行为模式)-很好的实现了对象的状态逻辑与动作实现的分类,状态逻辑在State的派生类实现,动作可以放在Context类中实现。
以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Context.h #pragma once class State; class Context { public: C ...