๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. ํฌ๊ฒŒ ๋งŒ๋“ค๊ธฐ (2812) [python][Gold III]

ttoance 2023. 2. 22. 22:45

๋ฌธ์ œ ๋งํฌ : https://www.acmicpc.net/problem/2812

>> ์ ‘๊ทผ๋ฐฉ๋ฒ•

- ์ˆซ์ž k๊ฐœ๋ฅผ ์ง€์› ์„๋•Œ ๋งจ ์•ž์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ํด์ˆ˜๋กœ ์ •๋‹ต์ธ ๊ฐ€์žฅ ํฐ ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

๋‚ด๊ฐ€ ์ฒ˜์Œ์— ํ’€์—ˆ๋˜ ์ ‘๊ทผ์€ ์ˆซ์ž ์ค‘์—์„œ ์ œ์ผ ํฐ ์ˆซ์ž๋“ค์„ ๋‚จ๊ธฐ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ–ˆ๋Š”๋ฐ,
๊ทธ๋Ÿด๊ฒฝ์šฐ ์„ธ๋ฒˆ์งธ ์˜ˆ์‹œ์—์„œ 477584๊ฐ€ ๋‚จ์œผ๋ฉด์„œ 775841์—์„œ ๋‹ต์ด ํ‹€๋ฆฐ๋‹ค.

 

- ์ด๋ฅผ ์œ„ํ•ด ํ˜„์žฌ ์ธ๋ฑ์Šค ์œ„์น˜๊ฐ’๋ณด๋‹ค ์™ผ์ชฝ(-1 ์œ„์น˜)๊ฐ’์ด ์ž‘๊ณ  ์ง€์šธ ์ˆ˜ ์žˆ๋Š” ํšŸ์ˆ˜ k๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด ํ˜„์žฌ ์ธ๋ฑ์Šค ์™ผ์ชฝ (-1) ๊ฐ’์„ ์ง€์›Œ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

>> ๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด

N, K = map(int, input().split())
num = input()


stack = []
cnt = K
for i in num:
    # ํ˜„์žฌ i๊ฐ€ stack์— ์žˆ๋Š” ๊ฐ’๋ณด๋‹ค ํฌ๋‹ค๋ฉด ์ œ๊ฑฐ 
    while (cnt > 0 and len(stack) > 0 and stack[len(stack) - 1] < i): 
        stack.pop(len(stack) - 1)
        cnt = cnt - 1
    stack.append(i)


answer = ''
for i in range(len(stack) - cnt):
    answer += stack[i]
print(answer)

 

>> <๋ณดํ†ต์˜ ์ทจ์ค€์ƒ์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ with ํŒŒ์ด์ฌ> ํ’€์ด p.173

N, K = map(int, input().split())
num = input()


answer = []
cnt = K
for i in num:
    while answer and cnt > 0 and answer[-1] < num:
        del answer[-1]
        cnt -= 1
    stack.append(i)


print(''.join(answer[:n-k]))

์ฑ…์—์„œ ์ œ๊ณตํ•˜๋Š” ํ’€์ด์—์„œ ๋ณด๋ฉด, ๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด์™€ ๋‹ค๋ฅด๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค.

1/ answer[len(stack) -1] => answer[-1]

cf1) ๋ฐฐ์—ด ์ธ๋ฑ์Šค ์Œ์ˆ˜ ์˜๋ฏธ  https://www.codingem.com/python-indexing-from-end/

 

2/ print(''.join(answer(\[:n-k\]))

cf2) ํŒŒ์ด์ฌ ์ฝ”๋”ฉ ๋„์žฅ : ์Šฌ๋ผ์ด์Šค ์‚ฌ์šฉํ•˜๊ธฐ  https://dojang.io/mod/page/view.php?id=2208

 

answer = ''
for i in range(len(stack) - cnt):
    answer += stack[i]
print(answer)

=> print(''.join(answer([:n-k]))

 

 

* 75% ์‹คํŒจ 

์ฐธ๊ณ ๋กœ, 75%์—์„œ ํ‹€๋ ธ์„ ๊ฒฝ์šฐ, ๋งˆ์ง€๋ง‰ joinํ•ด์ค„๋•Œ n-k๋งŒํผ ๊ฐ€์ ธ์˜ค๋Š” ๋ถ€๋ถ„์„ ์•ˆ ๋„ฃ์—ˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ํฐ๋ฐ, (๋‚ด๊ฐ€ ๊ทธ๋žฌ๋‹ค.)

์ด๋ถ€๋ถ„์€ ์•„๋ž˜ ์˜ˆ์‹œ์—์„œ ๊ฒฐ๊ตญ ๋งˆ์ง€๋ง‰๊นŒ์ง€ k ๊ฐœ ์ˆซ์ž๋ฅผ ์ž๋ฅด์ง€ ๋ชปํ–ˆ์„๋•Œ, ์•ž์—์„œ๋ถ€ํ„ฐ n-k๋งŒํผ ๊ฐ€์ ธ์˜ค๋Š” ๋ถ€๋ถ„์„ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

5 1
54321

๋‹ต : 5432

 

cf1) ๋ฐฑ์ค€ ๋ฐ˜๋ก€ ์˜ˆ์‹œ https://ideone.com/EWRsQr

cf2) ๋ฐฑ์ค€ ์งˆ๋ฌธ๊ฒŒ์‹œํŒ https://www.acmicpc.net/board/view/108820

 

 

 


์•„๋ž˜ ์ฑ… <๋ณดํ†ต์˜ ์ทจ์ค€์ƒ์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ with ํŒŒ์ด์ฌ> 7-6์ ˆ์„ ์ฝ๊ณ  ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค. 

http://www.yes24.com/Product/Goods/107478270

 

๋ณดํ†ต์˜ ์ทจ์ค€์ƒ์„ ์œ„ํ•œ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ with ํŒŒ์ด์ฌ - YES24

์ด ์ฑ…์€ ์†์— ์žกํžˆ๋Š” ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ํ•ฉ๊ฒฉ ๋ฐฉ๋ฒ•์„ ์ œ์‹œํ•œ๋‹ค. ๋ฐ”๋กœ “๋ฐฑ์ค€ ํ”Œ๋ž˜ํ‹ฐ๋„˜ 5 & ์ฝ”๋“œ ํฌ์Šค ํŒŒ๋ž€์ƒ‰ ๋žญํฌ”๋กœ ๋ชฉํ‘œ ์„ค์ •์„ ๊ตฌ์ฒดํ™”ํ•œ ๊ฒƒ์ด๋‹ค. ์ด ์ˆ˜์ค€์„ ๋‹ฌ์„ฑํ•˜๋ฉด ์›ฌ๋งŒํ•œ ๊ธฐ์—…์˜ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ

www.yes24.com

 

๋ฐ˜์‘ํ˜•