ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๋ฐ˜์‘ํ˜•

def solution(arrangement):
    answer = 0 # ์‡ ๋ง‰๋Œ€๊ธฐ ์กฐ๊ฐ ๋ณ€์ˆ˜
    stack = [] # ๋ฐฐ์—ด ์ž„์‹œ๋กœ ๋‹ด์„ ์Šคํƒ
    meet = False # )๋งŒ๋‚ฌ๋Š”์ง€ ํ™•์ธ ์—ฌ๋ถ€
    
    for arr in arrangement:
        # (๋ฅผ ๋งŒ๋‚  ๊ฒฝ์šฐ
        if arr == '(':
            # ์Šคํƒ์— ์ €์žฅ
            stack.append(arr)
            meet = False
        # )๋ฅผ ๋งŒ๋‚  ๊ฒฝ์šฐ
        else:
            # ์Šคํƒ์—์„œ ๊บผ๋ƒ„
            stack.pop()
            
            # )๊ฐ€ ์ฒ˜์Œ์œผ๋กœ ๋‚˜์˜ค๋Š”๊ฒฝ์šฐ : ๋ ˆ์ด์ € ์˜๊ธฐ
            if meet == False:
                answer += len(stack)
            # )๊ฐ€ ์—ฐ์†์œผ๋กœ ๋‚˜์˜ค๋Š”๊ฒฝ์šฐ : ๋ง‰๋Œ€๊ธฐ๊ฐ€ ๋๋‚จ
            else:
                answer += 1 
            
            # )๋ฅผ ๋งŒ๋‚ฌ์œผ๋‹ˆ True๋กœ ์ „ํ™˜
            meet = True
                
    return answer

 

 

๋ญ”๊ฐ€ ์ƒ๋‹นํžˆ ์‰ฝ๊ณ  ๋น ๋ฅด๊ฒŒ ์ž‘์„ฑํ•ด์„œ ํ•ด๊ฒฐํ•œ๊ฑฐ๊ฐ™์€๋ฐ .. ๋งž๋Š”๊ฐ€ ๋ชจ๋ฅด๊ฒƒ๋„ค

 

๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€