*[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 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...
随机推荐
- getOutputStream() has already been called for this response异常的原因和解决方法
今天在调试一个小web项目时,验证码不显示了,而且后台报错 getOutputStream() has already been called for this response 经过查找得知: 在t ...
- 阻止子View获取焦点方法
android:descendantFocusability:ViewGroup ep: android:descendantFocusability=blocksDescendants
- Fluent Validation For .NET
//.net 中数据验证,一个开源的项目,直接下载 1 using FluentValidation; public class CustomerValidator: AbstractValidato ...
- ubuntu 恢复gnome-panel
Ubuntu重启panel 的方法首先进入终端, 依次输入以下命令1.gconftool --recursive-unset /apps/panel2.rm -rf ~/.gconf/apps/pan ...
- intellij idea社区版 & maven & git & tomcat/jetty 的struts2项目的搭建
1.新建一个project,并在project下新建一个maven module.
- java_集合框架
一.集合框架图 二.Collection接口 Collection中可以存储的元素间无序,可以重复的元素. Collection接口的子接口List和Set,Map不是Collecti ...
- 暑假集训(2)第四弹 ----- 敌兵布阵(hdu1166)
D - 敌兵布阵 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64bit ...
- CENTOS7 使用网络管理器配置静态IP地址
CENTOS7 的网络配置和CENTOS6有些不同. 如果你想要使用网络管理器来管理该接口,你可以使用nmtui(网络管理器文本用户界面),它提供了在终端环境中配置配置网络管理器的方式. 在使用nmt ...
- redis_笔记
1.redis是什么 这个问题的结果影响了我们怎么用redis.如果你认为redis是一个key value store,那可能会用它来 代替mysql:如果认为它是一个可以持久化的cache,可能只 ...
- DataGridView如何快速导出Excel
从DataGridView或DataTable导出Excel文件,为了按照数据类型设置单元格格式,导出Excel时速度都比较慢,一直找不到好的办法. 最后从外文网站上找到解决办法,使用ws.get_R ...