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. Monggodb基础

    MongoDB 查询文档使用 find() 方法. find() 方法以非结构化的方式来显示所有文档. 语法 MongoDB 查询数据的语法格式如下: db.collection.find(query ...

  2. vmvare使用桥接和NAT方式连接网络

    一.背景:本着学以致用的心态,试着最小化安装Centos7.4.安装centos主要目的有两个:共享文件(samba).安装postgresql数据库 本打算使用内网(不联网)的方式安装samba和p ...

  3. MYSQL1

    一:对查询就行优化 避免全表查询 1.首先考虑在where及order by 列上建立索引 2.where子句   LIKE  '%abc%' 前置%   引擎放弃使用索引而进行全表扫描 3.wher ...

  4. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165304

    下载镜像文件 在官网上下载好64位的镜像文件后,按照网上是教程进行安装,安装成功后截图如下 接下来是安装增强功能 按照教程安装增强功能后截图如下 设置共享文件 安装搜狗 在安装搜狗时遇到了安装失败的情 ...

  5. v-charts 第一次亲密接触

    v-charts是什么鬼 v-charts是饿了么团队开源的一个图表库,vue+echarts开发.用element-ui直接集成echarts有些费劲,而v-charts已经封装成vue组件,可以直 ...

  6. 解析如何实现微信唤醒默认浏览器下载app教程!

    前言 现如今微信对第三方app下载链接的拦截是越来越严格了,下载链接在微信中分享转发经常会被拦截,一旦被拦截用户就只能复制链接手动打开浏览器粘贴才能访问,如此给用户带来的体验台差,用户量无法有效地累积 ...

  7. xcode打包命令

    xcodebuild clean -workspace Myproject.xcworkspace -scheme myProject xcodebuild archive -workspace My ...

  8. FireDac 同时连接SQLserver2000时出现 Connection is busy with results for another command

    First chance exception at $763FC632. Exception class EMSSQLNativeException with message '[FireDAC][P ...

  9. js导出excel文件

    <div id="tablesDiv"> <table id="tabDiv1"> <tbody><tr> &l ...

  10. Spring核心之IOC

    IOC是Spring的两大核心之一:IOC的核心就是解耦. 举个例子:有2个班级可以上课,校长指定老师去上课,代码如下 package com.hongcong.test; public class ...