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 width of such a ramp is j - i.
Find the maximum width of a ramp in A. If one doesn't exist, return 0.
Example 1:
Input: [6,0,8,2,1,5]
Output: 4
Explanation:
The maximum width ramp is achieved at (i, j) = (1, 5): A[1] = 0 and A[5] = 5.
Example 2:
Input: [9,8,1,0,1,9,4,0,4,1]
Output: 7
Explanation:
The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1.
Note:
2 <= A.length <= 500000 <= A[i] <= 50000
class Solution {
private int findLargestElementIndexNotLargerThan(int[] A, List<Integer> indexA, int target) {
int left = 0, right = indexA.size()-1;
while(left <= right) {
int mid = left + (right - left)/2;
if(A[indexA.get(mid)] == target) {
return indexA.get(mid);
}
else if(A[indexA.get(mid)] > target) {
left = mid + 1;
}
else {
right = mid-1;
}
}
return indexA.get(left);
}
public int maxWidthRamp(int[] A) {
List<Integer> indexA = new ArrayList<>();
int result = 0;
for(int i = 0; i < A.length; ++i) {
if(indexA.isEmpty() || A[indexA.get(indexA.size()-1)] > A[i]) {
indexA.add(i);
}
else {
int targetIndex = findLargestElementIndexNotLargerThan(A, indexA, A[i]);
result = Math.max(result, i - targetIndex);
}
}
return result;
}
}
binary search
class Solution {
private int findLargestElementIndexNotLargerThan(int[] A, List<Integer> indexA, int target) {
int left = 0, right = indexA.size()-1;
while(left < right) {
int mid = left + (right - left)/2;
if(A[indexA.get(mid)] > target) {
left = mid + 1;
}
else {
right = mid;
}
}
return indexA.get(left);
}
public int maxWidthRamp(int[] A) {
List<Integer> indexA = new ArrayList<>();
int result = 0;
for(int i = 0; i < A.length; ++i) {
if(indexA.isEmpty() || A[indexA.get(indexA.size()-1)] > A[i]) {
indexA.add(i);
}
else {
int targetIndex = findLargestElementIndexNotLargerThan(A, indexA, A[i]);
result = Math.max(result, i - targetIndex);
}
}
return result;
}
}
Idea 1.b. using treeSet in java, using floor to save writing binary search
class Solution {
public int maxWidthRamp(int[] A) {
Comparator<Integer> cmp = (a, b) -> Integer.compare(A[a], A[b]);
TreeSet<Integer> indexA = new TreeSet<>(cmp);
int result = 0;
for(int i = 0; i < A.length; ++i) {
if(indexA.isEmpty() || A[indexA.first()] > A[i]) {
indexA.add(i);
}
else {
int targetIndex = indexA.floor(i);
result = Math.max(result, i - targetIndex);
}
}
return result;
}
}
Idea 1.c using TreeMap with index, saving customerised comparator
class Solution {
public int maxWidthRamp(int[] A) {
TreeMap<Integer, Integer> indexA = new TreeMap<>();
int result = 0;
for(int i = 0; i < A.length; ++i) {
Integer targetValue = indexA.floorKey(A[i]);
if(targetValue == null) {
indexA.put(A[i], i);
}
else {
result = Math.max(result, i - indexA.get(targetValue));
}
}
return result;
}
}
Idea 2. buiding the decreasing array to maintain all the possible smaller candidates during the first loop of the array, during 2nd loop, scanning the array from right to left, the top of element is at least <= current number, this also explains why descending order, we need to look back for a smaller or equal value, a descending order stack can guarantee that the top element is always smaller or equal to the current element.
if A[stack.top()] <= A[right], there is no pair between stack.top() and right which could have bigger gap than right - stack.top(), hence stack pop(), continue
Time complexity: O(n)
Space compexity: O(n)
class Solution {
public int maxWidthRamp(int[] A) {
Deque<Integer> indexA = new ArrayDeque<>();
int result = 0;
for(int i = 0; i < A.length; ++i) {
if(indexA.isEmpty() || A[indexA.peek()] > A[i]) {
indexA.push(i);
}
}
for(int right = A.length-1; right+1 > result; --right) {
while(!indexA.isEmpty() && A[indexA.peek()] <= A[right]) {
result = Math.max(result, right - indexA.peek());
indexA.pop();
}
}
return result;
}
}
Idea 3. Sorted the array based on index, the maximum ramp ending at each index i = i - min(previousIndex), the smallest index which has smaller value
Time complexity: O(nlogn)
Space complexity: O(n)
class Solution {
public int maxWidthRamp(int[] A) {
List<Integer> indexA = new ArrayList<>();
for(int i = 0; i < A.length; ++i) {
indexA.add(i);
}
Comparator<Integer> cmp = (a, b) -> {
int c = Integer.compare(A[a], A[b]);
if(c == 0) {
return Integer.compare(a, b);
}
return c;
};
Collections.sort(indexA, cmp);
int minPrev = A.length;
int result = 0;
for(int index: indexA) {
result = Math.max(result, index - minPrev);
minPrev = Math.min(minPrev, index);
}
return result;
}
}
Maximum Width Ramp LT962的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 962. Maximum Width Ramp
本题题意: 在数组中,找到最大的j-i,使得i<j and A[i] <= A[j] 思路: 维持一个递减的栈,遇到比栈顶小的元素,进栈: 比大于等于栈顶的元素-> 找到栈中第一个小 ...
- 单调栈-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 ...
随机推荐
- windows cmd.exe 将程序 stdout 输出到文件中
问题背景:通过 cmd.exe 调用程序,会有一些输出信息,在 cmd 中不方便查阅,所以需要导入文件中. 例如 方法: 可以在其路径下看到
- 【手记】MTK之TASK创建及使用
首先来看看task的数据类型声明,在config\include\hal\task_config.h中对task和module类型进行了定义. /*************************** ...
- 进制与进制转换DAY2
进制和进制转换 一.进制的基础 1.十进制(案例) 系数:0-9 进位规则:逢十进一 权:基数的次幂 基数:几进制基数就是几 规律:右侧第一位的权是10的0次幂,每向左移动一位次幂会+1. 进制的表示 ...
- 运行UMAT:+ABQ和VS、IVF绑定
运行UMAT: 1.run-script----xxxx.py2.属性---编辑材料---通用---非独立变量---用户材料3.job---编辑作业---通用----用户子程序.for4.parall ...
- js中this的绑定规则及优先级
一. this绑定规则 函数调用位置决定了this的绑定对象,必须找到正确的调用位置判断需要应用下面四条规则中的哪一条. 1.1 默认绑定 看下面代码: function foo() { cons ...
- conda国内源的设置 by dwSun
conda国内源的设置 by dwSun anaconda是一个balabalabala... 知道这个软件的人肯定不用介绍,不知道的也不必介绍. conda是anaconda的包管理器,通过cond ...
- CamStar insitexmlclient重新封装为.net Core类库
工作原因经常使用camstar的 InsiteXMLClient类库做二次开发,但是只能在4.X环境下使用,对于日益繁荣的.net core生态,花费了些时间把原有的类库重新封装为.net core ...
- APK签名说明
在 Android 系统下, 一些公司会将自己做的APK进行管控,授权签名后方可使用. APK所属的软件公司会提供签名包,例如: 第一步:是要检查所操作的 PC 机是否安装 JDK,如果没有安装,请安 ...
- linux下用数据泵导入导出(impdp、expdp)
expdp和impdp expdp假设a用户的默认表空间是a,导出用户a所有数据: 如果是多实例 需要在命令行或终端手工指定实例 set ORACLE_SID=实例名 否则回报ORA-12560: T ...
- html 自定义上传图片样式,并回显
<div id="photoUpLoad"> <input type="file" id="photo" name=&qu ...