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

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  8. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  9. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

随机推荐

  1. OAuth2集成——《跟我学Shiro》

    http://jinnianshilongnian.iteye.com/blog/2038646 目前很多开放平台如新浪微博开放平台都在使用提供开放API接口供开发者使用,随之带来了第三方应用要到开放 ...

  2. gwt-问题解决

    最近在看gwt,写了个demo,但是总是出问题,困扰了好几天,后台也没报错,但就是加载不出来 第一次编译以后是可以的,但是改了代码后就不行了,后台也没报错,google了好长时间也没出来. 于是换了个 ...

  3. Android通过tcpdump抓包(wifi, 2g, 3g都可以)

    http://blog.csdn.net/deng529828/article/details/20646197 1. 手机要有root权限 2. 下载tcpdump   http://www.str ...

  4. Apache中关于页面缓存的设置

    http://www.cnblogs.com/yyyyy5101/articles/1899350.html Expires.Cache-Control.Last-Modified.ETag是RFC ...

  5. RDLC打印或导出Word的 分页设置 页边距和页面大小

    RDLC 导出Word的时候发现,Word的尺寸和页边距有问题,查了MSDN看到这样一段话 Page Sizing When the report is rendered, the Word page ...

  6. Object-C中的内存管理小记

    //错解1:内存泄露 - (void)setObj:(Object *)newObj { obj = [newObj retain]; } 当新旧对象指向不同时,执行这段代码后,obj会指向另一个对象 ...

  7. android 登陆案例_最终版本 sharedpreference

    xml  与之前的登陆案例相同 java代码: package com.itheima.login; import java.util.Map; import com.itheima.login.ut ...

  8. IOS上iframe的滚动条失效的解决办法

    #iframe-wrap { position: fixed; top: 100px; bottom: 0px; left: 0px; right: 0px; -webkit-overflow-scr ...

  9. eclipse导入包的快捷键

    在Eclipse里,写一个没有导入相应包的类名(这个类名已经完全写全,比如LayoutManager), 可以用ctrl+shift+M/Ctrl+Shift+o/Ctrl+1导入相应的包. 其中Ct ...

  10. What are the differences between small, minor, and major updates?

    Following contents are excerpted from the this website and only used for knowledge sharing:  Install ...