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 width of such a ramp is j - i.
Find the maximum width of a ramp in A. If one doesn't exist, return 0.
Example 1:
Input: [6,0,8,2,1,5]
Output: 4
Explanation:
The maximum width ramp is achieved at (i, j) = (1, 5): A[1] = 0 and A[5] = 5.
Example 2:
Input: [9,8,1,0,1,9,4,0,4,1]
Output: 7
Explanation:
The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1.
Note:
2 <= A.length <= 500000 <= A[i] <= 50000
直接的做法,O(nlog(n))。
class Solution {
public:
int maxWidthRamp(vector<int>& A) {
map<int, vector<int>> m;
for(int i=; i<A.size(); i++) m[A[i]].push_back(i);
int ret = -;
int prev_first = ;
for(auto it : m){
//cout << it.first << endl;
if(ret == -){
prev_first = it.second.front();
ret = max(ret, it.second.back() - prev_first);
}else{
prev_first = min(prev_first, it.second.front());
ret = max(ret, it.second.back() - prev_first);
}
}
return ret;
}
};
LC 962. Maximum Width Ramp的更多相关文章
- 【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 ...
- 962. Maximum Width Ramp
本题题意: 在数组中,找到最大的j-i,使得i<j and A[i] <= A[j] 思路: 维持一个递减的栈,遇到比栈顶小的元素,进栈: 比大于等于栈顶的元素-> 找到栈中第一个小 ...
- [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 ...
- 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 ...
- LC 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 ...
- 单调栈-Maximum Width Ramp
2020-01-23 19:39:26 问题描述: 问题求解: public int maxWidthRamp(int[] A) { Stack<Integer> stack = new ...
- 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 ...
随机推荐
- 命令行工具--curl
目录 命令:curl 一.简介 二.使用案例 1.基本用法 2.保存访问的网页 3.测试网页返回值 4.指定proxy服务器以及其端口 5.cookie 6.模仿浏览器 7.伪造referer(盗链) ...
- 记录旧手机(米6)安装Linux(Ubuntu16.04)踩过的坑
旧的小米6在抽屉吃灰半年,一直没想好要怎么处理,于是就想着安装Linux. 完整教程来自https://blog.csdn.net/Greepex/article/details/85333027 原 ...
- onItemSelected 获取选中的 信息 3种方法
@Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) ...
- 关于C++跨平台
问题:C++是怎么跨平台的呢? 答: 因为支持C++语言的各个平台的架构不同(比如CPU能够处理的指令集不一样),所以一份C++源代码要想在另一个操作系统平台上执行,就必须用该平台相对应的C++代码编 ...
- 吐槽一下jsoup
网络爬虫的本质就是通过域名加上特定的路由方式与远程资源建立一个短暂的连接,然后通过io流的方式读取.然后说一下jsoup,jsoup可以说是目前的爬虫工具包里面对java底层的工具类封装最简单的一种了 ...
- 201871010106-丁宣元 《面向对象程序设计(java)》第十七周学习总结
201871010106-丁宣元 <面向对象程序设计(java)>第十七周学习总结 正文开头: 项目 内容 这个作业属于哪个课程 https://home.cnblogs.com/u/nw ...
- nginx缓冲区,跳转,超时
缓冲区 nginx服务器向后台转发请求的时候, 往往只收到一下部分请求头的信息,缓冲区的意义在于尽可能多的保存请求信息. 语法配置 proxy _buffering打开后---设置为on,浏览器里面将 ...
- 利用fastjson的一些使用小记
//Json converter to objectTouBaoTXTpaicResponse touBaoTXTpaicResponse = JSON.parseObject(json.toJSON ...
- Python设置浏览器宽高
# 发起请求,设置浏览器宽高 # 代码中引入selenium版本为:3.4.3 # 通过Chrom浏览器访问发起请求 # Chrom版本:59 ,chromdriver:2.3 # 需要对应版本的Ch ...
- Visual Studio 编译Winform程序不生成.vshost.exe和.pdb文件的方法
问题:使用Visual Studio编译工程时,默认设置下,即使选择了「Release」时也会生成扩展名为「.vshost.exe」和「.pdb」的文件. 先解释一下各个文件的作用: .pdb文件: ...