Koala - 16기/코딩테스트 기초 스터디

[백준/Python] 17608번: 막대기

Jinn_12609 2024. 11. 17. 15:24

문제

 

https://www.acmicpc.net/problem/17608

 


Algorithm

첫 줄에 막대기의 개수 n을 입력 받는다

둘째 줄부터 n개의 막대기 길이를 입력받는다.

스택의 마지막을 1로 설정하여 마지막 막대기보다 긴 막대기가 있으면  count += 1을 하고 막대기의 길이를 가장 긴 막대기로 새로 저장한다.

 


 

 

Code

import sys

input = sys.stdin.readline

N = int(input())
stack = []

for _ in range(N):
    stack.append(int(input()))

last = stack[-1]
count = 1

for i in reversed(range(N)):
    if stack[i] > last:
        count += 1
        last = stack[i]

print(count)