public int solution(int[] H) {
        int blocks = 1;
        Stack stack = new Stack(H.length);
        stack.push(H[0]);

        for (int i = 1; i < H.length; i++) {
            if (stack.peek() < H[i]) {
                stack.push(H[i]); //increase of wall height, which adds the need of a new block
                blocks++;
            } else if (stack.peek() > H[i]) {
                while (stack.pop() > H[i]) { //pop elements until you reach lower or equal height
                    if (stack.isEmpty() || stack.peek() < H[i]) { //we reached lower height, so we can push our new height and we need a new block
                        stack.push(H[i]);
                        blocks++;
                        break;
                    } else if (stack.peek() == H[i]) break; //we reached same height, stop loop, no need of new block
                }
            }
        }
        return blocks;
    }