给定一个整数数组 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_20191112【闭包函数和装饰器】

    闭包函数 什么是闭包函数:闭包函数是函数嵌套.函数对象.名称空间和作用域的集合体. 闭包函数必须在函数内部定义,闭包函数可以引用外层函数的名字. # _*_ coding: gbk _*_ # @Au ...

  2. css----less预处理器

    ###less less是一种动态样式语言,属于css预处理器的范畴,它扩展了 CSS 语言, 增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展 LESS 既可以在 客户端 上运行 ,也 ...

  3. keep, preserve, noprune

    忘了紧急补充

  4. windows 嵌入控制台

    { 实际非常简单 需要控制台的hwnd 和 hdc 能获取控制台的hwnd 那hdc 就出来了 有了hdc 还有什么不能干的呢?? 如果会win32 窗口编程的就知道hdc,是一个让人流口水的类型 } ...

  5. spring mvc多环境下配置文件的设置

    在实际开发时经常需要把一些配置信息写在配置文件,比如mysql的主机地址.端口号.用户名和密码等.另外,在开发代码时可能用一套配置参数,而部署到测试环境时又会用另一套配置参数,测试完毕再部署到线上环境 ...

  6. socket2里面,有些函数带WSA开头,有些不带。请问有何区别?

    WSASocket可以使用WinSock特有功能,比如重叠IO,用dwflags指定.    WSA的A是指api,用于区别spi,因为在spi中还有wspsocket,wspaccept等... 在 ...

  7. CSS display overflow 属性 cursor光标类型

    display属性   功能:规则网页元素如何显示的问题.   取值:none(隐藏).block(以块元素显示).inline(以行内元素显示)   block:可以实现将行内元素转成块元素.   ...

  8. element table初始化默认选中以及切换页码的时候保留选中状态

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <!-- impor ...

  9. ideal的maven项目不小心remove module,如何找回

    找回方式: ideal的最右侧有个maven projects 点开后点击“+”,也就是Add Maven Projects,寻找不小心remove modules 的 子项目的pom文件所在的位置, ...

  10. 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919

    大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...