[codility]Stone-wall

// 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)