*[codility]StoneWall
https://codility.com/demo/take-sample-test/stone_wall

拼石块。用最少的方块。一开始想了想用贪心,是可行的,就是尽量每次把当前的高度往右延伸到最多,比如7,8,7,那么7往右延伸可以覆盖到第二个7,这样减掉,后面的就变成1,0了,问题仍然等价。
但这样要O(n^2)。结果需要O(n)的复杂度。这个时候就想到了单调栈。
于是栈中只记录递增的序列,发现比top当前的大就pop,因为这个对之后的已经没有作用了。因为每个元素都进栈出栈一次,平摊下来是O(n)的。
第一次自己想出单调栈的解法,开心。
// you can also use includes, for example:
// #include <algorithm>
#include <stack>
int solution(const vector<int> &H) {
// write your code in C++98
stack<int> st;
int count = 0;
for (int i = 0; i < H.size(); i++) {
while (!st.empty() && st.top() > H[i]) {
st.pop();
}
if (!st.empty() && st.top() == H[i]) {
continue;
}
else { // st.top < H[i] || st.empty()
count++;
st.push(H[i]);
}
}
return count;
}
*[codility]StoneWall的更多相关文章
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- GenomicRangeQuery /codility/ preFix sums
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...
- *[codility]Peaks
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- *[codility]AscendingPaths
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...
- *[codility]MaxDoubleSliceSum
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...
- *[codility]Fish
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...
- *[codility]CartesianSequence
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
随机推荐
- 【转载】Spark SQL之External DataSource外部数据源
http://blog.csdn.net/oopsoom/article/details/42061077 一.Spark SQL External DataSource简介 随着Spark1.2的发 ...
- ASP大数据量使用GetRows()提升速度
抽取10万条数据,Access数据库,GetRows() 现有10W条数据,Access数据库保存 通过正常提取: <% Set conn= Server.CreateObject(" ...
- 如何覆盖aar的资源
1.首先理解一下aar的构造 classes.jar ----代码 res---资源文件 2.替换 查看res里面的资源文件,这个资源文件事实上都是跟安卓的资源文件夹是一样的.你只需要理解xml和里面 ...
- 让aspx页面也可以通过url路由进行访问
参考文章:http://blog.csdn.net/zhanglong_longlong/article/details/8841030 这两天,在工作中需要将aspx的页面虚拟成url路径访问.比如 ...
- Extjs搜索域使用
要在使用的panel在预先加载搜索域类requires : ["Ext.ux.form.SearchField"],
- 知乎 zhihu
知乎上关于美食的精彩问答有哪些? http://www.zhihu.com/question/22744751/answer/22473212 知乎上关于乐队的精彩问答有哪些? http://www. ...
- nodejs-fs使用
(1)读取文本文件时须添加上'encoding'才能输出可读的内容. 02.txt hello,world! nodejs_readfile.js var fs = require('fs'); fs ...
- 13_ServletContext对象
[简介] ServletContext即Servlet上下文对象,该对象表示当前的web应用环境信息,一个Web应用只会创建一个ServletContext对象. Web容器启动的时候,它会为每个We ...
- eNSP
L2交换机 sysvlan 200q interface Vlanif 200ip address 192.168.0.1 24dis thisq interface Ethernet 0/0/1po ...
- 使用AutoMapper
一.AutoMapper初探: [参考Using AutoMapper: Getting Started] 1.新建空的ASP.NET MVC项目. 2.在Models文件夹添加类: public c ...