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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. 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), ...

  9. [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 ...

  10. [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 ...

随机推荐

  1. JNDI初认识

    JNDI即Java命名和目录接口,英文全称为Java Naming and Directory Interface,从字面上似乎十分晦涩,下面从理论和实际项目应用方面来阐述. 1.命名:在我们实际生活 ...

  2. sql server中分布式查询随笔

    由于业务逻辑的多样性 经常得在sql server中查询不同数据库中数据 这就产生了分布式查询的需求 现我将开发中遇到的几种查询总结如下: 1.access版本 --建立连接服务器 exec sp_a ...

  3. 使用struts+spring+hibernate组装web应用

    这篇文章将讨论怎样组合几个着名的框架去做到松耦合的目的,怎样建立你的构架,怎样让你的各个应用层保持一致.富于挑战的是:组合这些框架使得每一层都以一种松耦合的方式彼此沟通,而与底层的技术无关.这篇文章将 ...

  4. 创建Unity新项目并编译成游戏程序

    注:本人所使用的Unity版本为:Unity5.3.5f1,所使用的VS版本为:Visual.Studio.2013.Ultimate 折腾了快一个月了,终于有时间做自己的啦,哈哈: ) 步骤一:启动 ...

  5. 2016/7/7 自定义函数copy

    题目:输入整数n(n<=10000),表示接下来将会输入n个实数,将这n个实数存入数组a中.请定义一个数组拷贝函数将数组a中的n个数拷贝到数组b中. 分析: (1)输入n,再输入n个实数存入数组 ...

  6. OpenJudge / Poj 1928 The Peanuts C++

    链接地址:http://bailian.openjudge.cn/practice/1928 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 Mr. Robinson and h ...

  7. CentOS 安装jdk7

    1.下载jdk​​ http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html​ 选择jdk ...

  8. Linux – RedHat7 / CentOS 7 忘记root密码修改

    1.(a) 开机出现grub boot loader 开机选项菜单时,立即点击键盘任意鍵,boot loader 会暂停. (b) 按下’e’,编辑选项菜单(c) 移动上下鍵至linux16 核心命令 ...

  9. log4net 总结

    说实话,我并不是太想写这篇文章,因为我承诺过要完成博客园的部分功能,所以一直都在积极的利用下班时间来完善这个系统, 但是我又不想让看我源代码的朋友不知道我写的代码是什么意思,所以我还是单独写一个文章, ...

  10. AVAudioRecorder 录制音频

    AVFoundation 中使用AVAudioRecorder 类添加音频录制功能是非常简单的, AVAudioRecorder构建与Audio Queue Services之上是一个功能强大且代码简 ...