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. JAXB - XML Schema Types, Defining an Enumeration

    If you want a data type that enumerates discrete values you should use a restriction of the schema t ...

  2. linux下设置mysql数据库字符集utf8

    mysql中文乱码解决方法:将mysql数据库编码统一utf8 查看数据库编码: show variables like 'character%'; 编辑/etc/my.cnf [mysql] def ...

  3. 编码神器之sublime(插件安装)

    一款优秀的编辑器是程序员的左膀右臂,相信每一个程序员手边都有自己熟悉的编辑器. 从一开始使用sublime的时候就开始喜欢上了这款编辑器,被他强大的功能深深的吸引了. sublime的强大来源于他的扩 ...

  4. SQL Server(高级) 关键字的使用 二

    二, 高级 关键字 -- 使用介绍 8,Top 的使用(Top子句返回记录的数目) select top number|percent column_name(s) from table_name 或 ...

  5. Brackets 配置

    插件 Brackets Icons  左侧导航的文件图标 FuncDocr  注释工具 QuickDocsJS  js帮助文档 Beautify  格式化代码 Brackets Git  git支持 ...

  6. ### 线性回归(Regression)

    linear regression logistic regression softmax regression #@author: gr #@date: 2014-01-21 #@email: fo ...

  7. iOS开发,多个button数组,每个数组只能选中5项,多个数组只能选择3个。

    由于常用xib,所以不想用代码写那么多个button.而且也懒的算位置 直接xib拉线成四个数组.水果,零食,饮料,甜点. 入题实现的功能就是,在这四个数组之中只能在3个数组只选中5项.有点绕(就比如 ...

  8. 12天学好C语言——记录我的C语言学习之路(Day 7)

    12天学好C语言--记录我的C语言学习之路 Day 7: 昨天进行了一天的数组学习,今天大家可以先写几个昨天的程序热热身,回顾回顾,然后今天第一个新程序也是关于数组的,比较难,准备好就开始啦! //输 ...

  9. hibernate案例 测试代码

    测试staff数据表连接到maeclipse 在staff中插入一行 package com.hibernate.test; import org.hibernate.Session; import ...

  10. hanoi双塔

    汉诺塔,经典的递归. 经典的汉诺塔游戏相信很多同学都会玩的,规则就不用赘述,百科一下就OK.有三个柱子A,B,C,A柱子上套有n个大小不等的盘子,任意两个盘子,上面的盘子一定小于下面的盘子.现在请你编 ...