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

直接的做法,O(nlog(n))。

class Solution {
public:
int maxWidthRamp(vector<int>& A) {
map<int, vector<int>> m;
for(int i=; i<A.size(); i++) m[A[i]].push_back(i);
int ret = -;
int prev_first = ;
for(auto it : m){
//cout << it.first << endl;
if(ret == -){
prev_first = it.second.front();
ret = max(ret, it.second.back() - prev_first);
}else{
prev_first = min(prev_first, it.second.front());
ret = max(ret, it.second.back() - prev_first);
}
}
return ret;
}
};

LC 962. Maximum Width Ramp的更多相关文章

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

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

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

  3. 962. Maximum Width Ramp

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

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

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

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

  7. LC 662. 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 ...

  8. 单调栈-Maximum Width Ramp

    2020-01-23 19:39:26 问题描述: 问题求解: public int maxWidthRamp(int[] A) { Stack<Integer> stack = new ...

  9. 662. Maximum Width of Binary Tree二叉树的最大宽度

    [抄题]: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...

随机推荐

  1. 怎么处理系统蓝屏后提示代码0x000000d1的错误?

    电脑开机有时会出现蓝屏,导致蓝屏的原因有很多,每种错误都有不同的代码.下面就来和大家分享一下电脑开机蓝屏出现0x000000d1错误代码是什么原因?我们又该怎么去解决这个问题. 电脑开机蓝屏出现0x0 ...

  2. shell基本概念

    一.shell分类 常见shell:bash.zsh.tcsh linux默认:bash shell #tcsh #bash #pstree | grep login |- .. .. |-login ...

  3. Elasticsearch中Mapping

    映射(Mapping) 概念:创建索引时,可以预先定义字段的类型以及相关属性.从而使得索引建立得更加细致和完善.如果不预先设置映射,会自动识别输入的字段类型. 官方文档(字段数据类型):https:/ ...

  4. USRPX310 在GNU Radio上更改通道A或B

    UHD:USRP sink和USRP source默认是A通道发射接收.或设置 Mb0:Subdev Spec: A:0 更改为B通道收发:设置 Mb0:Subdev Spec: B:0

  5. 解决使用vue打包时vendor文件过大或者是app.js文件很大的问题

    这篇文章主要介绍了使用vue打包时vendor文件过大或者是app.js文件很大问题的解决方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下 第一次使用vue2.0开发,之前都是用的angu ...

  6. docker run always

    https://www.cnblogs.com/kaishirenshi/p/10396446.html

  7. ffmpeg 在vs配置的库名称

    avcodec.lib; avformat.lib; avutil.lib; avdevice.lib; avfilter.lib;postproc.lib;swresample.lib; swsca ...

  8. FZU-1901-Period 2(KMP)

    链接: https://vjudge.net/problem/FZU-1901 题意: For each prefix with length P of a given string S,if S[i ...

  9. 处理springboot OTS parsing error: Failed to convert WOFF 2.0 font to SFNT

    springboot项目中添加了字体等文件后,页面无法识别,浏览器调试窗口报错如下: Failed to decode downloaded font: http://localhost:8080/f ...

  10. MySQL错误:ERROR 1067 (42000): Invalid default value for 'timestamp_field'

    数据库报错   ERROR 1067 (42000): Invalid default value for 'start_time' 是因为数据库的配置有问题: 可以看到  NO_ZERO_IN_DA ...