给定一个整数数组 A,坡是元组 (i, j),其中  i < j 且 A[i] <= A[j]。这样的坡的宽度为 j - i。

找出 A 中的坡的最大宽度,如果不存在,返回 0 。

示例 1:

输入:[6,0,8,2,1,5] 输出:4 解释: 最大宽度的坡为 (i, j) = (1, 5): A[1] = 0 且 A[5] = 5.

示例 2:

输入:[9,8,1,0,1,9,4,0,4,1] 输出:7 解释: 最大宽度的坡为 (i, j) = (2, 9): A[2] = 1 且 A[9] = 1.

提示:

  1. 2 <= A.length <= 50000
  2. 0 <= A[i] <= 50000

优化的暴力法,超时

class Solution {
public:
int maxWidthRamp(vector<int>& A)
{
int len = A.size();
int MAX = 0;
for(int i = 0; i < len && len - i > MAX; i++)
{
for(int j = len - 1; j > i && (j - i) > MAX; j--)
{
if(A[j] >= A[i])
{
MAX = max(MAX, j - i);
}
}
}
return MAX;
}
};

用滑块超时

class Solution {
public:
int maxWidthRamp(vector<int>& A)
{
int len = A.size();
int i = len - 1;
while(i > 0)
{
int left = 0;
int right = i;
while(right < len)
{
if(A[left] <= A[right])
{
return right - left;
}
else
{
left++;
right++;
}
}
i--;
}
return 0;
}
};

终于过了:

bool cmp(pair<int, int> x, pair<int, int> y)
{
if(x.first == y.first)
return x.second < y.second;
return x.first < y.first;
} class Solution {
public:
int maxWidthRamp(vector<int>& A)
{
int len = A.size();
vector<pair<int, int> > ary;//val and pos
for(int i = 0; i < len; i++)
{
ary.push_back(make_pair(A[i], i));
}
sort(ary.begin(), ary.end(), cmp);
int MAX = 0;
int left = ary[0].second;
for(int i = 1; i < len; i++)
{
MAX = max(ary[i].second - left, MAX);
left = min(left, ary[i].second);
}
return MAX;
}
};

Leetcode962. Maximum Width最大宽度坡 Ramp的更多相关文章

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

  2. [LeetCode] 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 ...

  3. [Swift]LeetCode662. 二叉树最大宽度 | 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 ...

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

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

  6. [LeetCode] 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 ...

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

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

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

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

随机推荐

  1. leetcode-212-单词搜索②

    题目描述: 第一次提交:(超出时间限制) class Solution: def findWords(self, board: List[List[str]], words: List[str]) - ...

  2. 阿里云启动视频云V5计划,全面赋能生态合作伙伴

    9月25 - 27日,主题为数·智的2019云栖大会在杭州举行.在第三天的智能视频云专场中,阿里云研究员金戈首次对外发布视频云V5计划,释放视频IT基础设施红利,赋能生态合作伙伴,共促大视频产业发展. ...

  3. C++——类的继承(派生)

    类的继承就是子类可以拥有父类的成员变量和成员函数 //public 修饰的成员变量 方法 在类的内部 类的外部都能使用//protected: 修饰的成员变量方法,在类的内部使用 ,在继承的子类中可用 ...

  4. 基于V8引擎的C++和JS的相互交互

    基于什么原因略! 1. 脚本引擎的基本功能 V8只是一个JS引擎.去除它的特点功能出处,它必须要实现JS引擎的几个基础功能: 脚本执行:脚本可能是一个表达式:一段js代码:或者一个文件执行表达式返回j ...

  5. Hexo 博客图片添加至图床---腾讯云COS图床使用。

    个人博客:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io 腾讯云官网 登录注册 创建存储桶 进入上面的存 ...

  6. centos7 将home目录空间扩容到根目录

    [root@localhost ~]# umount /home/ [root@localhost ~]# lvremove /dev/mapper/centos-home Do you really ...

  7. ie6-8 avalon2 单页应用项目实战备忘

    坑爹的ie,作为小组leader,尼玛,小伙伴儿们不乐意做的事情,我来做好了..心累... 如果,各位同学有定制开发ie6-8版本的需求,还是尽量不要用单页应用模式了,也不要用avalon这类mvvm ...

  8. demjson处理json数据

    因为json数据不规范出现了以下问题: json.decoder.JSONDecodeError: Expecting property name enclosed in double quo 网上查 ...

  9. 第五周课堂笔记1th

    可迭代对象   Isinstance  判断一个对象是否属于某种类型 接受两个参数 迭代器 以下数据类型都没迭代器: 把没有迭代器的类型更改为有迭代器类型 用迭代器进行取值: 判断迭代器的方法: 3. ...

  10. 面试系列14 redis的过期策略都有哪些

    (1)设置过期时间 我们set key的时候,都可以给一个expire time,就是过期时间,指定这个key比如说只能存活1个小时?10分钟?这个很有用,我们自己可以指定缓存到期就失效. 如果假设你 ...