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. 【HQL】函数汇总

    背景 抽空整理一篇HQL函数及常用的小技巧 COALESCE COALESCE(T v1, T v2, -) 返回参数中的第一个非空值:如果所有值都为NULL,那么返回NULL

  2. mysql查看及设置最大连接数

    #查看: show variables like '%max_connections%'; #设置: set GLOBAL max_connections = 1000;

  3. Spring MVC配置实例

    1.下载Jar文件,添加到项目 lib文件夹中. 使用eclipse新建 Web 项目.下载导入相关的 jar 和 Tomcat.我的java版本是JDK1.8 对应的 Tomcat 版本是 8.0. ...

  4. 创建Oracle表空间

    *分为四步 */ /*第1步:创建临时表空间 */ create temporarytablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i\ ...

  5. java Thread 类的源码阅读(oracle jdk1.8)

    java线程类的源码分析阅读技巧: 首先阅读thread类重点关注一下几个问题: 1.start() ,启动一个线程是如何实现的? 2.java线程状态机的变化过程以及如何实现的? 3. 1.star ...

  6. ThreeJs 模型的缩放、移动、旋转 以及使用鼠标对三维物体的缩放

    首先我们创建一个模型对象 var geometry = new THREE.BoxGeometry( 100, 100, 100); //边长100的正方体 var material = new TH ...

  7. JEECG3.8 全套实战视频全部开放,免费下载!

    JEECG快速开发平台V3.8版本自去年10月份发布以来,下载使用数屡创新高,并受到众多开发者积极反馈.为帮助更多初学者能够快速上手,JEECG V3.8版本实战教程现已全面开放,免费下载!本教程深入 ...

  8. cobbler登录web界面时出现“Internal Server Error”

    当进行cobbler配置后,并进行web登录时,出现错误: 先查看其日志位置 #cat /etc/httpd/conf.d/ssl.conf 在其中位置发现其错误的日志位置为/etc/httpd/lo ...

  9. Django ORM中常用字段和参数

    一些说明: 表myapp_person的名称是自动生成的,如果你要自定义表名,需要在model的Meta类中指定 db_table 参数,强烈建议使用小写表名,特别是使用MySQL作为后端数据库时. ...

  10. (英文版)VScode一键生成.vue模板

    1. 安装vscode,官网地址 2.安装一个插件,识别vue文件 插件库中搜索Vetur,下图中的第一个,点击安装(Install) 3.新建代码片段 点击Code(代码)-Preferences( ...