Codility - StoneWall

RESULT

import java.util.*;

class Solution {
    public int solution(int[] H) {
        int count = 0;
        Stack<Integer> stack = new Stack<>();
        
        for (int h : H) {
            while (!stack.empty() && stack.peek() > h) {
                stack.pop();
            }
            
            if (stack.empty() || stack.peek() != h) {
                count++;
            }
            
            stack.push(h);
        }
        
        return count;
    }
}