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. Oracle笔记(四) 简单查询、限定查询、数据的排序

    一.简单查询 SQL(Structured Query Language) 结构化查询语言,是一种数据库查询和程序设计语言,用于存取数据以及查询.更新和管理关系数据库系统.ANSI(美国国家标准学会) ...

  2. LNMP安装与配置之Python3

    环境 我们是在CentOS7下安装python3,但CentOS已经默认安装了Python2,而 Yum 等工具依赖原来的Python2.所以我们需要稍作配置让Python2与Python3可以共存. ...

  3. 第六章 组件 55 组件-使用components定义私有组件

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. oracle修改已存在数据的字段类型

    第一次使用oracle数据库,在通过Navicat premium工具修改字段类型时,发现报“ORA-01439: column to be modified must be empty to cha ...

  5. 【Winform-自定义控件】自定义控件学习+一个笑脸控件例子

    1.CompositeControls组合控件:在原有控件的基础上根据需要进行组合 2.ExtendedControls 扩展控件:继承自原有控件,添加一些新的属性和方法,绘制一些新元素 当每个But ...

  6. fegin熔断autowired失败

    在SpringBootApplication中加入 @EnableFeignClients(basePackages = "com.supplychain")指定熔断的路径就可以了

  7. Blade 模板

    在Laravel 5.3中,@foreach指令提供了更加强大的功能,在每一个@foreach循环体中都可以调用一个新的$loop变量.该变量是一个stdClass实例,包含了当前循环的元数据信息,让 ...

  8. redis异步处理

    $reids = new Redis; $redis->connect('localhost',6379); $redis->auth(''); //将数组转换成字符串再存到redis中 ...

  9. .NET面试题系列(二十一)C#中Equals和==的比较

    序言 值类型的比较 ; ; Console.WriteLine("Equals和= =(等于号)的比较"); Console.WriteLine("i.Equals(j) ...

  10. Sql Server 基本使用

    一.登录sql server数据库 1.若需要连接本机数据库服务器,服务器名可以采用“local”.“.”“本机Ip”. 2.在连接sql server 之前,确保sql server服务已经启动,如 ...