给定一个整数数组 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. CSIC_716_20191105【数字、字符串、列表】

    python数据类型及其内置方法 一.整型:主要用于数学运算 其他进制----->转十进制 """ 其他进制转换为十进制 通过int('x进制数',x)实现 &qu ...

  2. python 之单例模式

    单例模式1 单例=>只有一个单例2 静态方法+静态字段3 所有实例中等转的内容相同时 用单例模式class Sqllite: __instance=None def __init__(self) ...

  3. chrome控制台使用jquery

    html页面中加入:<script type="text/javascript" src="http://static.fanxian.com/script/jqu ...

  4. C++——类的多继承

    多继承概念 一个类有多个直接基类的继承关系称为多继承 多继承声明语法 class  派生类名 : 访问控制  基类名1 ,  访问控制  基类名2 ,  … , 访问控制  基类名n { 数据成员和成 ...

  5. 9.1 mongo_python.py

    # 安装 pymongo pip install pymongo import pymongo try: # 1.链接mongod的服务 mongo_py = pymongo.MongoClient( ...

  6. C++函数或者命名空间前面加::

    命名空间和函数前面加上:: 经常看到命名空间前就只有:: 比如  ::test;这种代表是全局的test 比如  ::CreateDirectory(..),代表使用系统API也就是全局的 避免使用到 ...

  7. winform 旋转图片

    //img.RotateFlip(RotateFlipType.Rotate90FlipNone); //顺时针旋转90度 RotateFlipType.Rotate90FlipNone //逆时针旋 ...

  8. 【转帖】WebRTC回声抵消模块简要分析

    webrtc 的回声抵消(aec.aecm)算法主要包括以下几个重要模块:回声时延估计:NLMS(归一化最小均方自适应算法):NLP(非线性滤波):CNG(舒适噪声产生).一般经典aec算法还应包括双 ...

  9. Laravel移除Cache-Control

    碰到一个问题,网站上线后,需要移除Cache-Control,就是下面这个东西 方案1 失败 参考网址:https://stackoverflow.com/questions/51821563/lar ...

  10. React报错:Laravel React-Router browserHistory 刷新子页面报错404

    举例:myblog.com/ 刷新没问题 myblog.com/add 刷新404 browserHistory报404,hashHistory却正常 原因是少路由.web.php添加路由 Route ...