알고리즘/힙

[Python] 백준 2841 외계인의 기타 연주

JayAlex07 2023. 3. 25. 08:00

🧑‍💻 [Python] 백준 2841 외계인의 기타 연주

Silver 1 - 스택

![img](https://blog.kakaocdn.net/dna/cC1MR5/btr1QwKHa2P/AAAAAAAAAAAAAAAAAAAAAA2oJDQCegmgXIKOgwKexdLo2rhEcOq5iPyGHKlS9m1D/img.png?credential=yqXZFxpELC7KVnFOS48ylbz2pIh7yKj8&expires=1753973999&allow_ip=&allow_referer=&signature=goM%2FON96QEdVuRC%2BekQM9Z7Ns0I%3D)

 

 

코드

N, M = map(int, input().split())

stack = [[] for _ in range(7)]

count = 0
for _ in range(N):
    l, p = map(int, input().split())

    while stack[l] and stack[l][-1] > p:
        stack[l].pop()
        count += 1

    if not stack[l] or stack[l][-1] < p:
        stack[l].append(p)
        count += 1

print(count)