목차

풍선 터트리기

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호68646
문제명풍선 터트리기
레벨Level 3
분류

애드혹

시간복잡도O(n)
인풋사이즈n<=1,000,000
사용한 언어Python
해결날짜2020/12/07

풀이

코드

코드 1 - 일반적 코드

"""Solution code for "Programmers 68646. 풍선 터트리기".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/68646
- Solution link: http://www.teferi.net/ps/problems/programmers/68646
"""

INF = float('inf')


def solution(a):
    answer = 0
    min_num = INF
    for num in a:
        if num < min_num:
            answer += 1 
            min_num = num
            
    min_num = INF
    for num in reversed(a):
        if num < min_num:
            answer += 1 
            min_num = num
            
    return answer - 1

코드 2 - 컴팩트 코드

"""Solution code for "Programmers 68646. 풍선 터트리기".

Compact code using walrus operater in list comprehension.
- Problem link: https://programmers.co.kr/learn/courses/30/lessons/68646
- Solution link: http://www.teferi.net/ps/problems/programmers/68646
"""

INF = float('inf')


def solution(a):
    answer = 0
    min_num = INF
    answer += len([(min_num := num) for num in a if num < min_num])
    min_num = INF
    answer += len([(min_num := num) for num in reversed(a) if num < min_num])
    return answer - 1