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 ...
随机推荐
- postman报InvalidArgumentException
解决方案:
- postgresql使用pg_dump和pg_restore 实现跨服务器的数据库迁移或备份
因为业务需求,需要将服务器上的postgre多个数据库的数据整个库得迁移到另一个postgre数据库上. 一般表较少时,会使用postgre 的copy to 和 copy from 命令就能完成表的 ...
- Apache虚拟目录实现同一个IP绑定多个域名
在前:我使用的是Xampp,所以路径可能不同 找到apache\conf\extra\httpd-vhosts.conf, 如果没有的话请自己新建httpd-vhosts.conf文件, 并且在htt ...
- java在使用equals的时候一种习惯帮忙隔离大部分空指针
相同的一段代码: 所以,在平时编码的时候,尽量把需要比对的不为空的放在equal前面,比如判断 i 是否等于 1,那么使用 1.equals(i) 更为合适,在equals源码中 传入的是object ...
- MYSQL - database 以及 table 的增删改查
MYSQL - database 以及 table 的增删改查 MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这 ...
- for双重循环中的结构分离(语法结构问题)
//增加搜索列表 function addSearchList(){ $.get("/mall/h5_get_search_list.html","",func ...
- ASP.NET的Validform验证表单使用说明
当我们写提交表单的时候往往需要验证表单是否填写了内容,是否正确,这个插件可以很方便的完成我们需要的验证! 使用方法: 1.引用JS <script type="text/javascr ...
- HDU-1852-Beijing 2008-一个神奇的公式求逆元
As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a lit ...
- threading线程中的方法(27-11)
t1.start() # 执行线程 t1.join() # 阻塞 t1.setDaemon(True) #守护线程 threading.current_thread() # 查看执行的是哪一个线程 t ...
- axios解决调用后端接口跨域问题
vue-cli通过是本地代理的方式解决接口跨域问题的.但是在vue-cli的默认项目配置中这个代理是没有配置的,如果现在项目中使用,必须手动配置config/index.js文件 ... proxyT ...