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. HTTP 协议中 URI 和 URL 有什么区别?

    HTTP 协议中 URI 和 URL 有什么区别? HTTP = Hyper Text Transfer ProtocolURI = Universal Resource IdentifierURL ...

  2. dubbo 相关面试题 有用

    调用关系说明: · 0. 服务容器负责启动,加载,运行服务提供者. · 1. 服务提供者在启动时,向注册中心注册自己提供的服务. · 2. 服务消费者在启动时,向注册中心订阅自己所需的服务. · 3. ...

  3. ShopNc登录

  4. Entity Framework 6.0 Tutorials(6):Transaction support

    Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a trans ...

  5. MFC可视化

    当你修改了变量的值,而希望对话框控件更新显示,就应该在修改变量后调用UpdateData(FALSE):如果你希望知道用户在对话框中到底输入了什么,就应该在访问变量前调用UpdateData(TRUE ...

  6. C# JSON使用的常用技巧(二)

    JSON在php里一句json_encode就可以得到 在C#里我们同样也很容易的可以得到 用到的类库:Newtonsoft.Json.dll 实体类: class Cat { public stri ...

  7. 编写高质量代码改善C#程序的157个建议——建议31:在LINQ查询中避免不必要的迭代

    建议31:在LINQ查询中避免不必要的迭代 无论是SQL查询还是LINQ查询,搜索到结果立刻返回总比搜索完所有的结果再将结果返回的效率要高. 示例代码: class MyList : IEnumera ...

  8. C#中Winform程序中如何实现多维表头【不通过第三方报表程序】

    问题:C#中Winform程序中如何实现多维表头. 在网上搜了很多方法,大多数方法对于我这种新手,看的都不是很懂.最后在新浪博客看到了一篇比较易懂的文章:[DataGridView二维表头与合并单元格 ...

  9. springcloud 实现微服务间调用

    package com.idoipo.ibt.config; import org.apache.http.HttpException; import org.apache.http.HttpRequ ...

  10. Redis缓存穿透、缓存雪崩

    缓存穿透 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义. ...