๐ŸŒฑ → ๐ŸŒณ

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํฐ ์ˆ˜ ๋งŒ๋“ค๊ธฐ - python ๋ณธ๋ฌธ

Algorithms

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํฐ ์ˆ˜ ๋งŒ๋“ค๊ธฐ - python

BAY 2023. 9. 15. 13:50
728x90

์ฝ”๋“œ

์‹œ๊ฐ„์ดˆ๊ณผ ์ฝ”๋“œ

from itertools import combinations

def solution(number, k):
    answer = ''
    arr = list(combinations(number, len(number)-k))
    for i in max(arr):
        answer += i
    return answer

combination ์ž์ฒด๊ฐ€ ๋ฌด๋ฆฌ์ธ ๋“ฏ ํ•˜๋‹ค

 

์ •๋‹ต ํ’€์ด 1

def solution(number, k):
    stack = [number[0]]
    for num in number[1:]:
        while len(stack) > 0 and stack[-1] < num and k > 0:
            k -= 1
            stack.pop()
        stack.append(num)
    if k != 0:
        stack = stack[:-k]
    return ''.join(stack)

 

์ •๋‹ต ํ’€์ด 2

from collections import deque
def solution(number, k):
    q = deque()
    stack = deque()

    for n in number:
        q.append(n)

    while q and k:
        while stack and stack[-1] < q[0]:
            stack.pop()
            k -= 1
            if not k:
                break
        stack.append(q.popleft())

    while k and stack:
        stack.pop()
        k -= 1

    return "".join(stack + q)
728x90