1일1알고/Java Algorithm

[23/09/10] B26042

kim chan jin 2023. 9. 10. 21:31

peek 과 poll 의 차이

peek 은 구조체에서 제거하지 않고 반환

poll 은 구조체에서 제거한 후 반환

 

package D1;

import java.util.*;

// 막대기
public class B17608 {
    public static void main(String[] args) {

        Stack<Integer> stack = new Stack<>();

        Scanner sc = new Scanner(System.in);

        int testCase = sc.nextInt();
        int count = 1;

        for(int i=0; i<testCase; i++) {
            stack.push(sc.nextInt());
        }

        int popped = stack.pop();
        while(!stack.empty()) {
            if(stack.peek() > popped) {
                count++;
                popped = stack.pop();
                continue;
            }
            stack.pop();
        }

        System.out.println(count);
    }
}