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. JavaJDBC【四、存储过程的使用】

    Mysql还没学到存储过程,不过语法比较简单 此处不深究数据库中的存储过程怎么创建,后面在mysql的学习笔记里再做整理 今天只整理java中如何调用存储过程 语句 CallableStatement ...

  2. 命令行工具--curl

    目录 命令:curl 一.简介 二.使用案例 1.基本用法 2.保存访问的网页 3.测试网页返回值 4.指定proxy服务器以及其端口 5.cookie 6.模仿浏览器 7.伪造referer(盗链) ...

  3. hadoop--大数据生态圈中最基础、最重要的组件

    hadoop是什么? hadoop是一个由Apache基金会所开发的分布式系统基础架构,hdfs分布式文件存储.MapReduce并行计算.主要是用来解决海量数据的存储和海量数据的分析计算问题,这是狭 ...

  4. Decrator 装饰模式

    注:装饰模式是结构型设计模式,动态地给一个对象增加一些额外的职责,就增加的功能来说,Decorator模式相比生成子类更加灵活. 主要是利用组合代替继承来实现业务功能,对动态改变业务实现来说,相比更灵 ...

  5. 修改Jenkins目录

    注意:在Jenkins运行时是不能更改的. 请先将Jenkins停止运行. 1.windows环境下更改JENKINS的主目录 Windows环境中,Jenkins主目录默认在C:\Documents ...

  6. 函数参数-undefined-默认值-可选参数

    1.函数参数为undefined时,触发参数默认值 2.实参数量 < 形参数量,多余的形参值为 underfined 3.设置可选参数 1)JS中: 检测 undefined :function ...

  7. 嵌入式Linux下CAN总线配置

    问题背景:本人开发板使用的是迅为iTOP4412精英版,额外购买的CAN/485模块,如下图: 但是插上模块之后,在终端使用ifconfig can0命令发现开发板读不到CAN设备,显示“ifconf ...

  8. POJ-3080-Blue jeans(KMP, 暴力)

    链接: https://vjudge.net/problem/POJ-3080#author=alexandleo 题意: 给你一些字符串,让你找出最长的公共子串. 思路: 暴力枚举第一个串的子串,挨 ...

  9. zsh使用技巧(WIP)

    看了这个问题也关注这个问题很久了,很多人介绍的都是oh-my-zsh(一下简称omz)的一些特征.不可否认这是一个很优秀的框架,但zsh的世界不应该全是omz.这个回答,我想介绍一下zsh原生自带的一 ...

  10. MySQL下载与安装、解决报错

    MySQL下载与安装 一.下载 1.登录Mysql官网:https://www.mysql.com/ 2.点击下载Mysql社区版本 3.点击下载 4.点击下载Mysql社区服务 5.下载地址: 历史 ...