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:

  1. 2 <= A.length <= 50000
  2. 0 <= A[i] <= 50000
 Idea 1. building decreasing array, binary search the largest element not smaller than the target(the other elements in the array).
Time complexity: O(nlogn)
Space complexity: O(n)
 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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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 ...

  4. 【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]. ...

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

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

  6. 962. Maximum Width Ramp

    本题题意: 在数组中,找到最大的j-i,使得i<j and A[i] <= A[j] 思路: 维持一个递减的栈,遇到比栈顶小的元素,进栈: 比大于等于栈顶的元素-> 找到栈中第一个小 ...

  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. python:windows下python2.7安装mysql-python失败【转】

    转自:https://www.cnblogs.com/yujiebingqing/p/9633476.html 当我们尝试用: 1 pip install mysql-python #并不是MySQL ...

  2. 71.纯 CSS 创作一个跳 8 字型舞的 loader

    原文地址:https://segmentfault.com/a/1190000015534639#articleHeader0 感想:rotateX() 和rotateZ()一起使用好懵呀. HTML ...

  3. python-day2列表、元祖、字典;编码;字符串

    @导入模块时,会先搜索目前路径的同名py文件,再去全局环境变量找 @看模块的环境变量 import sys print(sys.path) @site-package存放第三方库,可以把自己建的拷贝在 ...

  4. Mysql将查询结果合并为一列

    利用函数:group_concat(),实现一个ID对应多个名称时,原本为多行数据,把名称合并成一行. 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order B ...

  5. Android Jetpack 组建介绍(二)——Lifecycler

    参考Android Jetpack架构组件之 Lifecycle(源码篇) 源码分析 关于Lifecycle的使用考上一篇文章Android Jetpack框架之 Lifecycles(使用篇),从使 ...

  6. 操作 实例 / dom

    响应式:数据改变时会触发其他联动.例如:模板中的数据绑定:计算属性的重新计算: ---------------------------------------------------- vm.$par ...

  7. Base64图片转Blob对象

    //将Base64图片转成Blob对象 //@args: base64Url:编码字符串,contentType:类型. function base64UrltoBlob(base64Url, con ...

  8. MM-移动类型

    链接:SAP移动类型   移动类型 备注 业务类型 SAP中事务代码 备注 101 采购订单收货.生产订单收货 收货 migo CO11N顶层处理移动类型\跨工厂收货 102 采购订单收货冲销 收货 ...

  9. 如何在Chrome中导入和导出密码

    如果想让 Chrome 支持密码导入和导出,需要先在地址栏中执行  chrome://flags/#password-import-export  将该功能启用并重启浏览器才能生效. 浏览器重启完成后 ...

  10. 服务主机superFetch占用磁盘过多

    有用户升级到Win10系统后,发现系统运行不快,查看任务管理器中正在运行的进程,然后查看到有一个名称为“服务主机:本地系统(网络受限)”的进程的磁盘占用率很高.如果想要解决这个问题,那么不妨参考下面介 ...