// you can also use includes, for example:
// #include <algorithm>
#include <stack>
int solution(const vector<int> &H) {
// write your code in C++98
//...using queue + greedy
int blockCnt = 0;
stack<int> wallStack;
int stackWallHeight = 0;
for(int i = 0; i < H.size(); ++i)
{
int curWallLen = H[i];
while(stackWallHeight > curWallLen)
{
stackWallHeight -= wallStack.top();
wallStack.pop();
}
if(stackWallHeight < curWallLen)
{
wallStack.push(curWallLen-stackWallHeight);
stackWallHeight += curWallLen-stackWallHeight;
blockCnt++;
}
}
//...return result
return blockCnt;
}
[codility]Stone-wall
最新推荐文章于 2019-08-27 13:08:42 发布