알고리즘 풀이/Codility

Codility - StoneWall (Stacks and Queues)

배게 2019. 12. 13. 16:07
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    public int solution(int[] H) {
        // write your code in Java SE 8
        
        Stack<Integer> st = new Stack<>();
        
        int answer = 0;
        
        for(int h : H){
            while(!st.isEmpty() && st.peek()>h){
                st.pop();
            }
            
            if(st.isEmpty() || st.peek()<h){
                st.push(h);
                answer++;
            }
        }
        
        return answer;
    }
cs