Leetcode962. Maximum Width最大宽度坡 Ramp
给定一个整数数组 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.
提示:
- 2 <= A.length <= 50000
- 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- 【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]. ...
- 【LeetCode】962. Maximum Width Ramp 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址:https://leetco ...
随机推荐
- Erlang学习记录:转义
转义 转义序列 含义 整数编码 \b 退格符 8 \d 删除符 127 \e 换码符 27 \f 换页符 12 \n 换行符 10 \r 回车符 13 \s 空格符 32 \t 制表符 9 \v 垂直 ...
- leetcood学习笔记-82-删除排序链表中的重复元素二
题目描述: 方法一: class Solution(object): def deleteDuplicates(self, head): """ :type head: ...
- Windows cd
显示当前目录名或改变当前目录. CHDIR [/D] [drive:][path]CHDIR [..]CD [/D] [drive:][path]CD [..] .. 指定要改成父目录. 键入 C ...
- CSS——滑动门技术及应用
先来体会下现实中的滑动门,或者你可以叫做推拉门: 滑动门出现的背景 制作网页时,为了美观,常常需要为网页元素设置特殊形状的背景,比如微信导航栏,有凸起和凹下去的感觉,最大的问题是里面的字数不一样多,咋 ...
- SQLServer日期格式化及创建相关日期
DECLARE @FirstDay_M DATETIME --本月初日期 ,); DECLARE @LastDay_M DATETIME --本月末日期 SET @LastDay_M = DATEAD ...
- MR/hive/shark/sparkSQL
shark完全兼容hive,完全兼容MR,它把它们替代.类SQL查询,性能比hive高很多 sparkSQL比shark更快.shark严重依赖hive,hive慢,无法优化. SparkSQL和sh ...
- Kafka和RabbitMQ 对比
1) Kafka成为业界大数据松耦合架构,异步,队列 特点:吞吐量高50m/s. Kafka和RabbitMQ都是MQ机制,它差异 Kafka作为大数据产品,可以作为数据源,也可以作为结果数据中转 ...
- Java-Maven-pom.xml-project-dependencies:dependencies
ylbtech-Java-Maven-pom.xml-project-dependencies:dependencies 1.java 调用ddl <!-- java 调用ddl --> ...
- (37)C#Linq
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/let-clause 一.定义 Linq(Lang ...
- python paramiko模块学习分享
python paramiko模块学习分享 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Sola ...