Problem Solving/BOJ & Programmers

[BOJ] 2493๋ฒˆ: ํƒ‘

geum 2022. 7. 18. 13:17

๋ฌธ์ œ

 

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๋ฉด '๋‚ด ์™ผ์ชฝ ์ธ๊ทผ์— ์žˆ์œผ๋ฉด์„œ ๋‚˜๋ณด๋‹ค ๋†’์€ ํƒ‘์˜ ์ธ๋ฑ์Šค ์ถœ๋ ฅ'์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค. '์ธ๊ทผ'์ด๋ผ๋Š” ํ‘œํ˜„์„ ์“ด ์ด์œ ๋Š” ๋†’์ด๊ฐ€ 4์ธ ๋‹ค์„ฏ๋ฒˆ์งธ ํƒ‘์ฒ˜๋Ÿผ ์™ผ์ชฝ์— ์žˆ๋Š” ํƒ‘๋“ค ์ค‘ ๋‚˜๋ณด๋‹ค ๋†’์€ ํƒ‘์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 7๋ณด๋‹ค ๋” ๋†’์€ ํƒ‘์ด ์žˆ์ง€๋งŒ 4๊ฐ€ ์œ ๋ ˆ์ด์ € ์‹ ํ˜ธ๋Š” ์ด๋ฏธ 7์— ๋‹ฟ์•˜์œผ๋ฏ€๋กœ 7์˜ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ+1์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

ํ’€์ด

โ˜ ์ฒซ ๋ฒˆ์งธ ์‹œ๋„(์‹œ๊ฐ„ ์ดˆ๊ณผ)

import sys
from collections import deque

N = int(input())

tower = list(map(int, sys.stdin.readline().split()))

received = [0]

for i in range(1, N):
    candidate = deque(tower[:i])
 
    flag = 0
    
    if max(candidate) <= tower[i]:
        received.append(0)
    else:
        while flag == 0:
            if candidate[-1] < tower[i]:
                candidate.pop()
            else:
                flag = tower.index(candidate.pop())+1                
                received.append(flag+1)
            
print(*received)

 

์ฝ”๋“œ ํ๋ฆ„ ๊ฐ„๋‹จ ์„ค๋ช…์šฉ ์ ‘์€๊ธ€

๋”๋ณด๊ธฐ

1. ํƒ‘ ๊ฐœ์ˆ˜๋งŒํผ for๋ฌธ์„ ๋Œ๋ฆฌ๋Š”๋ฐ ๊ฐ for๋ฌธ ์•ˆ์—์„œ ์Šฌ๋ผ์ด์‹ฑ์„ ์ด์šฉํ•ด i๋ฒˆ์งธ ํƒ‘๊ณผ ๋น„๊ตํ•  ํƒ‘ ๋ชฉ๋ก(candidate) ์ƒ์„ฑ

2. candidate์˜ max ๊ฐ’์ด ํ˜„์žฌ ํƒ‘ ๋†’์ด๋ณด๋‹ค ์ž‘์„ ๊ฒฝ์šฐ ๋ ˆ์ด์ €๋ฅผ ์ˆ˜์‹ ํ•  ํƒ‘์ด ์—†๋‹ค๋Š” ๋œป์ด๊ธฐ ๋•Œ๋ฌธ์— ์ •๋‹ต ์ €์žฅ์šฉ ๋ฐฐ์—ด์— 0 append

3-1. ํ˜„์žฌ ํƒ‘์˜ ๋ ˆ์ด์ € ์‹ ํ˜ธ๋ฅผ ๋ฐ›์„ ํƒ‘ ํ™•์ธ์šฉ flag๊ฐ€ 0์ผ ๋•Œ๋งŒ while๋ฌธ ๋ฐ˜๋ณต

3-2. candidate์˜ ๋งˆ์ง€๋ง‰ ํƒ‘ ๋†’์ด๊ฐ€ ํ˜„์žฌ ํƒ‘ ๋†’์ด๋ณด๋‹ค ๋‚ฎ์„ ๊ฒฝ์šฐ candidate์—์„œ ๋นผ๋ฒ„๋ฆฌ๊ณ  ๋’ค์—์„œ๋ถ€ํ„ฐ ํƒ‘ ๋†’์ด ์ฒดํฌ

 

์ด ํ๋ฆ„์—์„œ ํฌ๊ฒŒ ๋ณ€ํ•˜์ง€ ์•Š๊ณ  ๋ช‡ ๋ฒˆ ๋” ์‹œ๋„ํ•ด๋ดค๋Š”๋ฐ๋„ ๊ณ„์† ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋–ด๋‹ค. 'for๋ฌธ์ด ํ•˜๋‚˜ ๋ฐ–์— ์—†๋Š”๋ฐ ์–ด๋””์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๊ฑธ๋ฆฌ์ง€?' ์ด ์ƒ๊ฐ์„ ๊ณ„์† ํ•ด๋ณด๋‹ค๊ฐ€ ์Šค์Šค๋กœ ๋‹ต์„ ์ฐพ์ง€ ๋ชปํ•ด ๋ฐฑ์ค€ ์งˆ๋ฌธ ๊ฒ€์ƒ‰๋ž€๊ณผ ๊ตฌ๊ธ€์˜ ๋„์›€์„ ๋ฐ›์•˜๋‹ค.

 

์ •๋ง ์ƒ๊ฐ์ง€๋„ ๋ชปํ–ˆ๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ์˜ ์›์ธ์€ ์Šฌ๋ผ์ด์‹ฑ์ด์—ˆ๋‹ค. N๊ฐœ์˜ ์š”์†Œ์— ๋Œ€ํ•ด ๋งค๋ฒˆ ์Šฌ๋ผ์ด์‹ฑ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์ด๋Š” ๊ฒƒ๊ณผ ๋‹ฌ๋ฆฌ ์‹ค์งˆ์ ์ธ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N^2)์ด ๋œ๋‹ค๊ณ  ํ•œ๋‹ค. ํ˜ผ์ž์„œ๋Š” ์ ˆ๋Œ€ ๋ชป ์•Œ์•„์ฑ˜์„ ๋“ฏ

 

 

๐Ÿ–– ๋„ค ๋ฒˆ์งธ ์‹œ๋„(์Šคํƒ ํ™œ์šฉ, ์„ฑ๊ณต)

import sys
from collections import deque

N = int(sys.stdin.readline().rstrip())

tower = list(map(int, sys.stdin.readline().split()))

stack = []
received = [0 for _ in range(N)]

for i in range(N):
    while stack:
        if stack[-1][1] <= tower[i]:
            stack.pop()
        else:
            received[i] = stack[-1][0]
            break
         
    stack.append([i+1, tower[i]])

print(*received)

 

ํŒŒ์ด์ฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์Šคํƒ์€ append(), pop()๋งŒ ์“ธ ์ค„ ์•Œ๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์ง€ ์ œ๋Œ€๋กœ ํ™œ์šฉํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ํƒ‘ ์ธ๋ฑ์Šค์™€ ๋†’์ด๋ฅผ ํ•œ๊บผ๋ฒˆ์— ์ €์žฅํ•ด๋†“๊ณ  ์Šคํƒ์˜ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊บผ๋‚ด๋ฉด์„œ ํ˜„์žฌ ๋†’์ด์™€ ๋น„๊ตํ•ด์ฃผ๋ฉด ์Šฌ๋ผ์ด์‹ฑ์„ ์“ธ ๋•Œ์ฒ˜๋Ÿผ ๋งค๋ฒˆ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ํ•ญ์ƒ ์ƒ๊ฐ์„ ํ•˜๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์ž ๐Ÿ˜–