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

题意在解释里说的很清楚了,就看怎么想。

首先保证后面的数字比前面的是大于等于关系,这个排序可以搞定,然后到这个数为止,前面数字的位置里最小的数是谁,减去就好了

struct P{
int num;
int pos;
}H[];
bool cmp(P a,P b){
if(a.num == b.num){
return a.pos<b.pos;
}else{
return a.num<b.num;
}
}
class Solution {
public: int maxWidthRamp(vector<int>& A) {
int len = A.size();
for(int i=;i<len;i++){
H[i].num = A[i];
H[i].pos = i;
}
sort(H,H+len,cmp);
int Min = H[].pos;
int sum = -;
int cum = ;
for(int i=;i<len;i++){
//cout<<H[i].pos<<" "<<Min<<endl;
if(Min > H[i].pos){
Min = H[i].pos;
}else{ cum = H[i].pos - Min;
sum = max(sum,cum);
} }
if(sum == -){
sum = ;
}
return sum;
}
};

116th LeetCode Weekly Contest Maximum Width Ramp的更多相关文章

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

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

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

  3. 116th LeetCode Weekly Contest N-Repeated Element in Size 2N Array

    In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeate ...

  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. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

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

  7. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  8. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

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

随机推荐

  1. [原创]SQL 把表中字段存储的逗号隔开内容转换成列表形式

    我们日常开发中,不管是表设计问题抑或是其他什么原因,或多或少都会遇到一张表中有一个字段存储的内容是用逗号隔开的列表. 具体效果如下图: ------> 从左边图转换成右边图,像这种需求,我们难免 ...

  2. 448. Find All Numbers Disappeared in an Array 寻找有界数组[1,n]中的缺失数

    [抄题]: Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice ...

  3. Docker学习笔记_下载镜像更换为国内源,实现快速下载image

    1.编辑/etc/docker/daemon.json,增加下面内容: { "registry-mirrors": ["https://registry.docker-c ...

  4. Docker学习笔记_安装和使用nginx

    一.软件环境 1.宿主机OS:Win10 64位 2.虚拟机OS:Ubuntu 18.04,虚拟机IP:192.168.8.25 3.Docker安装在虚拟机Ubuntu 18.04上 二.安装过程 ...

  5. sql去除重复记录 且保留id最小的 没用

    第一步:查询重复记录   SELECT * FROM TableName   WHERE RepeatFiled IN (   SELECT RepeatFiled   FROM TableName ...

  6. 安装 SQL Server 2014 Express

    安装 SQL Server 2014 Express 我的电脑系统: Windows 10 64位 一 . 下载 安装Microsoft SQL Server 2014 Express 软甲下载地址: ...

  7. 19. AUTO INCREMENT 字段

    Auto-increment 会在新记录插入表中时生成一个唯一的数字. AUTO INCREMENT 字段 我们通常希望在每次插入新记录时,自动地创建主键字段的值. 我们可以在表中创建一个 auto- ...

  8. (转)使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享

    原文地址:http://www.cnblogs.com/huyong/archive/2013/09/24/3334848.html 使用Jquery+EasyUI 进行框架项目开发案例讲解之一 员工 ...

  9. [转]xe6 android 使用距离传感器(Proximiry)

    The first step it's a run sample from RAD Studio that named SensorInfo on your device. On the tab Bi ...

  10. MVC - Routing - 网址路由

    1. Routing  : 路由 主要是比对通过浏览器传来的http要求与响应适当的网址给浏览器. @Html.ActionLink("关于","About", ...