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 ...
随机推荐
- ORM简单增删改查
namespace ORM { class Program { static void Main(string[] args) { //AddPetStore();//添加 UpdatePetStor ...
- mysql 关于字符串搜索的函数
与like作用相似 FIND_IN_SET LOCATE POSITION
- django-spirt 论坛主题
官方文档 新建项目 django_sprit 文件夹 cd django_sprit pip install django-spirit spirit startproject mysite cd m ...
- redis编译和安装出现错误
redis编译到时候出现错误,记录一下原因 1.下载redis,https://redis.io/download ,一般选择稳定版本,稳定版的版本号是偶数,当前最新版本是5.0.4,Linux可直接 ...
- DA_06_iptables 与 firewalld 防火墙
8.1 防火墙管理工具 防火墙作为公网与内网之间的保护屏障,在保障数据的安全性方面起着至关重要的作用.主要功能都是依据策略对穿越防火墙自身的流量进行过滤.防火墙策略可以基于 流量的源目地址.端口号.协 ...
- 【转载】softmax的log似然代价函数(求导过程)
全文转载自:softmax的log似然代价函数(公式求导) 在人工神经网络(ANN)中,Softmax通常被用作输出层的激活函数.这不仅是因为它的效果好,而且因为它使得ANN的输出值更易于理解.同时, ...
- Android异常与性能优化相关面试问题-其他优化面试问题详解
Android不用静态变量存储数据: 静态变量等数据由于进程已经被杀死而被初始化.在Android中应用进程不是安全的,因为它会有系统给kill掉,但是在实际中可能会有这样的一个假象:当app被杀掉之 ...
- 转:ubuntu添加用户adduser,并给予sudo权限
ubuntu添加用户adduser,并给予sudo权限 如何创建ubuntu新用户? 首先打开终端,输入:sudo adduser username正在添加用户“username”... 正在添加新组 ...
- MySQL_DBA整理
MySQL_DBA整理(转) 2011-01-26 16:51:41 分类: Mysql/postgreSQL 转自:丁丁:http://www.itpub.net/thread-831154-2- ...
- jenkins 中MultiJob Phase的使用,简单的pipeline可以用这个写