[codility]Array-closest-ascenders
http://codility.com/demo/take-sample-test/pi2012
又是一道单调栈的题目。首先这道题目n^2是最朴素的做法。继续优化,因为和顺序有关就不好排序。然后,看到可以分解成求左方向最值和右方向最值的问题。此时要变成O(n)就要么贪心,要么DP,要么单调栈,要么有更多规律未发现。
这里有这么一个特点,考虑求左方向最值,当先有4,再有5之后,那么之前的4就没有意义了。所以用单调栈。如果用函数,代码可以更简洁。
#include <stack>
vector<int> solution(const vector<int> &A) {
// write your code in C++98
int len = A.size();
vector<int> R_left(len, 0);
stack<int> left_stack;
for (int i = 0; i < A.size(); i++) {
while (!left_stack.empty() && A[left_stack.top()] <= A[i]) {
left_stack.pop();
}
if (!left_stack.empty()) {
R_left[i] = abs(left_stack.top() - i);
}
left_stack.push(i);
} vector<int> R_right(len, 0);
stack<int> right_stack;
for (int i = A.size() - 1; i >= 0; i--) {
while (!right_stack.empty() && A[right_stack.top()] <= A[i]) {
right_stack.pop();
}
if (!right_stack.empty()) {
R_right[i] = abs(right_stack.top() - i);
}
right_stack.push(i);
}
vector<int> R(len);
for (int i = 0; i < len; i++) {
if (R_right[i] == 0 && R_left[i] == 0) R[i] = 0;
else if (R_right[i] == 0) R[i] = R_left[i];
else if (R_left[i] == 0) R[i] = R_right[i];
else R[i] = min(R_left[i], R_right[i]);
}
return R;
}
[codility]Array-closest-ascenders的更多相关文章
- LeetCode第[16]题(Java):3Sum Closest 标签:Array
题目难度:Medium 题目: Given an array S of n integers, find three integers in S such that the sum is closes ...
- K Closest Numbers In Sorted Array
Given a target number, a non-negative integer k and an integer array A sorted in ascending order, fi ...
- Closest Number in Sorted Array
Given a target number and an integer array A sorted in ascending order, find the index i in A such t ...
- Leetcode Array 16 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [array] leetCode-16. 3Sum Closest -Medium
16. 3Sum Closest -Medium descrition Given an array S of n integers, find three integers in S such th ...
- Array + two points leetcode.16 - 3Sum Closest
题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...
- leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST
1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...
- 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST
▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...
- [LeetCode] Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- eclipse怎么切换SVN的用户
在用eclipse的时候会经常用到SVN来进行代码的版本控制,为了方便起见,我们会保存密码,从此之后就不会再出现输入或者修改用户名和密码的地方了,这时候想切换用户怎么办,在本地操作的一种方法是删除SV ...
- 编程中的 if ()else() 语句
例句 if() else() double d = Convert.ToDouble(Console .ReadLine()); if (d >= 60 && d< ...
- Thread多线程stopSleep顺序问题
今天呢,学习了javase多线程,里面的睡眠sleep问题有点困扰: public class Thread_06_stopSleep{ public static void main(String[ ...
- C#与C++相比较之STL篇(续一)
本篇接<C#与C++相比较之STL篇>,主要探索C++STL的两个组件:算法和仿函数,以及C#的linq和拉姆达表达式.委托. STL的算法与仿函数 算法是个庞大的主题,STL包含了超过1 ...
- jxl和poi处理excel之比较
功能需求是根据客户提供的excel模板,程序动态填充其中的一些数据.该模板包含大量的宏,刚拿到的时候头都要晕了,本人虽天天和电脑打交道,但是excel咱不是高手啊,什么宏,之前光听过,听着都觉得是高级 ...
- LAMP虚拟主机配置以及控制目录访问
3.基于域名的虚拟主机配置 NameVirtualHost192.168.3.32:80#apache2.2.xx版本需要开启此选项,而且要和下面的保持一致:2.4.x版本就不需要此项设置了 < ...
- python 中函数的参数
一.python中的函数参数形式 python中函数一般有四种表现形式: 1.def function(arg1, arg2, arg3...) 这种是python中最常见的一中函数参数定义形式,函数 ...
- Unity3d Shader开发(三)Pass(Fog )
雾参数用于雾命令控制. 雾化是通过混合已生成的像素的颜色和基于到镜头的距离来确定的一个不变色来完成.雾化不会改变已经混合的像素的透明度值,只是改变RGB值. Syntax 语法 Fog { Fog C ...
- 实现n皇后问题(回溯法)
/*======================================== 功能:实现n皇后问题,这里实现4皇后问题 算法:回溯法 ============================= ...
- 利用 runtime,解决多次点击相同 button,导致重复跳转的问题-b
当app有点卡的时候,多次点击相同的button,经常出现,跳转了N次相同的界面(比如闲鱼) 解决办法 用运行时和分类,替换 UIControl 响应事件,根据响应的间隔时间来判断是否执行事件. 详细 ...