๊ธ€ ์ž‘์„ฑ์ž: ํƒ์‹œ ์šด์ „์‚ฌ
๋ฐ˜์‘ํ˜•

๐Ÿ”ข ํฐ ์ˆ˜ ๋งŒ๋“ค๊ธฐ

๐Ÿ˜ƒ ๋‚˜์˜ ์ฝ”๋“œ

def solution(number, k):
    length = len(number)
    if length > k:
        m = 0
        for cnt in range(k):
            for idx in range(m, length-1):
                if number[idx] < number[idx+1]:
                    number = number[:idx] + number[idx+1: ]
                    length -= 1
                    if idx > 0:
                        m = idx-1
                    break
            else:
                number = number[:length-k+cnt]
                break
        return "".join([str(i) for i in number])
    else:
        return "0"

ํƒ์š•๋ฒ•Greedy ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ํ•ด๋‹นํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ํƒ์š•๋ฒ•์„ ์ ์šฉํ•˜๊ณ ๋„, ํŠน์ • ์ผ€์ด์Šค(ํŠนํžˆ 10๋ฒˆ ์ผ€์ด์Šค)์—์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋กœ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜ ํŠน์ • ์˜ˆ์™ธ ์ผ€์ด์Šค๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์‹œ๊ฐ„์„ ์ตœ๋Œ€ํ•œ ์ค„์—ฌ์•ผํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์—์„œ ๊ฐ€์žฅ ๋ณต์žก๋„๊ฐ€ ํฐ ์˜ˆ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

number = "1234567890"*100000
k = 999999

์ฒ˜์Œ์—๋Š” ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ๋น ๋ฅด๊ฒ ์ง€๋ผ๋Š” ์ƒ๊ฐ๊ณผ pop์„ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๋น ๋ฅด๊ฒ ์ง€๋ผ๋Š” ์ƒ๊ฐ์— [int(i) for i in number] ๋กœ ์‹œ์ž‘ํ•˜๊ณ  list ๋Š” pop ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งฐ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ, ์•„๋ฌด๋ฆฌ ์ตœ์ ํ™”๋ฅผ ํ•ด๋„ ์ผ์ • ์‹œ๊ฐ„ ์ดํ•˜๋กœ ์ค„์–ด๋“ค ์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ( ์‚ฌ์‹ค ํ•ต์‹ฌ์„ ์ฐพ์•„๋‚ด์ง€ ๋ชปํ•˜๋ฉด ์ด๋Ÿฐ ์ตœ์ ํ™”๋Š” ์•„๋ฌด๋ฆฌํ•ด๋„ ์‹œ๊ฐ„๋‚ญ๋น„์ž…๋‹ˆ๋‹ค. )

๊ฒฐ๊ตญ ๋‹ค๋ฅธ ํ’€์ด๋ฅผ ๋ณด์•˜๊ณ , ๊ฒฐ๋ก ์„ ๋งํ•˜์ž๋ฉด list์˜ ๊ธธ์ด๊ฐ€ ๋ฌด์ˆ˜ํžˆ ๊ธธ์–ด์ง€๋ฉด, ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ๋ฌธ์ž๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ๋” ๋น ๋ฆ…๋‹ˆ๋‹ค. ์‹ฌ์ง€์–ด ๋ฌธ์ž์—ด์€ slicing๊ณผ์ •๊นŒ์ง€ ํฌํ•จ๋˜์—ˆ์ง€๋งŒ, ๊ทธ๊ฑธ ํ•˜๊ณ ์„œ๋„ ์ด ์ „์— ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ ์ผ€์ด์Šค๋ณด๋‹ค ์‹œ๊ฐ„์ด ์•ฝ 2๋ฐฐ ๋นจ๋ž์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ Problem Solving ๋ฌธ์ œ์˜ ๋ชฉ์ ์— ๋งž์ถ”๋ ค๋ฉด ์ œ๊ฐ€ ํ•œ ์ตœ์ ํ™” ์ •๋„์—์„œ๋Š” ๋‹ต์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒŒ ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ œ๊ฐ€ ์ƒ๊ฐํ•œ ์˜ˆ์™ธ ์ผ€์ด์Šค๋„ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ๋“ค์–ด๊ฐ€์ง€ ์•Š์•„์„œ ๋ฌธ์ œ์— ๊ฐœ์„ ์ด ์ข€ ํ•„์š”ํ•˜๋‹ค๊ณ  ๋Š๊ปด์ง‘๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•