L2 : 큰 수 만들기 Python

jhyunn·2023년 1월 20일
0

Programmers

목록 보기
46/69

L2 : 큰 수 만들기 Python

https://school.programmers.co.kr/learn/courses/30/lessons/42883

def solution(number, k):
    stack = []
    for n in number:
        # 이유는 모르겠지만 이 순서로 조건을 배치하지 않으면 문제가 생김
        while k>0 and len(stack)>0 and stack[-1]<n: #
            stack.pop()
            k -= 1
        stack.append(n)
    return ''.join(stack[:len(stack)-k])

stack에 앞에서부터 쌓으면서,
다음 수가 이전 수보다 크면 pop한다.

k가 남으면 뒤에서 자른다.

#그리디 #greedy

profile
https://github.com/Sungjeonghyun

0개의 댓글