[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 ...
随机推荐
- Java .Net C++ RSA 加密
原文:http://www.codeproject.com/Articles/25487/Cryptographic-Interoperability-Keys DEMO: JAVA .Net C++
- VBA实现随意输入组合码,查询唯一标识码
记录背景: 需要在excel中查询出组合码,对应的唯一标识码. 举例 组合码:4+5+6+9+1*2 标识码:A1 界面随意输入组合码:1*2+4+5+6+9 输出标识码:A1 VBA实现: P ...
- Android通过tcpdump抓包(wifi, 2g, 3g都可以)
http://blog.csdn.net/deng529828/article/details/20646197 1. 手机要有root权限 2. 下载tcpdump http://www.str ...
- xadmin学习笔记(二)——改造Django教程实例(1)
前言 xadmin是基于Python和Django的管理框架,想要能够熟练使用,学习Django是必须的.在学习Django的过程中,不妨用xadmin来验证下新的效果是怎样的.本文就是在学习Djan ...
- ios常用的一些类库
在网上收集到的 一:第三方插件 1:基于响应式编程思想的oc 地址:https://github.com/ReactiveCocoa/ReactiveCocoa 2:hud提示框 地址:https:/ ...
- Objective-C 学习笔记(Day 2)
------------------------------------------- 如何根据题目准确完整清晰的声明一个类并实现给定的行为 /* //下面这个程序教大家如何根据题目去声明一个类,并 ...
- [PR & ML 1] [Introduction] Informal Basic Concepts
最近还没更完OpenCV又开了新坑,谁教machine learning处在紧急又重要的地位呢.更新的内容总结自Pattern Recognition and Machine Learning by ...
- QT 常用设置
博文都写在了云笔记里面了,见谅,不想维护两个版本. QT 常用设置
- 九度OJ 1505 两个链表的第一个公共结点 【数据结构】
题目地址:http://ac.jobdu.com/problem.php?pid=1505 题目描述: 输入两个链表,找出它们的第一个公共结点. 输入: 输入可能包含多个测试样例. 对于每个测试案例, ...
- C/C++代码检视实例
相关文章链接如下: 微软过桥问题与测试人员素养 等价类分法 新解 测试用例设计中的NP难题 90%程序员写不出无BUG的二分查找程序? C/C++代码检视要点 4.1 代码检视 ...