// 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 发布
本文介绍了一种使用栈数据结构解决柱状图中所需砖块数量问题的方法。通过遍历输入的柱状图高度数组并利用栈来跟踪当前的砖块高度,实现了动态调整砖块数量以构建完整柱状图的功能。

623

被折叠的 条评论
为什么被折叠?



