[Programmers] ์‡ ๋ง‰๋Œ€๊ธฐ

Updated:

์‡ ๋ง‰๋Œ€๊ธฐ

์‡ ๋ง‰๋Œ€๊ธฐ ๋ฅผ ํด๋ฆญํ•˜๋ฉด ๋ฐ”๋กœ ์ด๋™ํ•œ๋‹ค.

๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐ ํ•ด ๋ดค๋Š”๋ฐ ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๊ฐ์ž ๋ง‰๋Œ€๊ธฐ ์‚ฌ์ด์˜ ๋ ˆ์ด์ € ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด ์กฐ๊ฐ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•,

๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ๋ ˆ์ด์ €๊ฐ€ ๋‚˜์˜ฌ ๋•Œ ๋งˆ๋‹ค ๊ทธ ๋ ˆ์ด์ € ์œ„์— ์žˆ๋Š” ๋ง‰๋Œ€๊ธฐ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•ด ์กฐ๊ฐ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

๋‘ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ ์•„๋ฌด๋ฆฌ ์ƒ๊ฐํ•ด๋„ ๊ทœ์น™์ด ๋ฐœ๊ฒฌ์ด ์•ˆ๋˜์—ˆ๋‹ค !!!

๊ทธ๋ž˜์„œ ์šฐ์„  ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ดค๋‹ค.

def solution(arr):
	answer = 0
	arr = arr.replace('()', 'L')
	stick = list()

	for val in arr:
		if val == '(':
			stick.append(0)
		elif val == ')':
			input = stick.pop() + 1
			answer += input

		elif val == 'L':
			for idx, cnt in enumerate(stick):
				stick[idx] = cnt + 1

	answer += input
	return answer

() ๋ฅผ ๋ ˆ์ด์ €๋กœ ๋ฐ”๊ฟ” ์ค€ ๋’ค, ๋ ˆ์ด์ €๊ฐ€ ๋‚˜์˜ฌ ๋•Œ ๋งˆ๋‹ค stick ์— ์กด์žฌํ•˜๋Š” ๋ชจ๋“  ์Šคํ‹ฑ์˜ cnt ๋ฅผ ๋Š˜๋ ค ์ฃผ์—ˆ๋‹ค.

๊ฒฐ๊ณผ๋Š” 20๊ฐœ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ค‘ ํ•œ ๊ฐœ๊ฐ€ ์‹œ๊ฐ„์ดˆ๊ณผ์˜€๋‹ค..

๋ง‰๋Œ€๊ฐ€ ๋งŽ์ด ๊ฒน์ณ์žˆ๊ณ  ๊ทธ ์œ„์— ๋ ˆ์ด์ €๊ฐ€ ๊ณ„์† ์žˆ๋Š” ๊ฒฝ์šฐ ์ฒซ ๋ง‰๋Œ€๊ธฐ๋ถ€ํ„ฐ ๊ณ„์† ๊ฐ’์„ ์˜ฌ๋ ค์ค˜์•ผ ํ•ด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

์ € ์ฝ”๋“œ์—์„œ ๋” ์ด์ƒ ์‹œ๊ฐ„์„ ์ค„์ผ ๋ฐฉ๋ฒ•์€ ์—†๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์–ด ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ ๋ฐ”๊พธ๊ธฐ๋กœ ํ–ˆ๋‹ค..

์ƒ๊ฐ๋ณด๋‹ค ๊ทœ์น™์ด ์•„์ฃผ์•„์ฃผ ๊ฐ„๋‹จํ–ˆ๋Š”๋ฐ ์ƒ๊ฐํ•˜๋Š”๋ฐ ๊นŒ์ง€ ์กฐ๊ธˆ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ๋‹ค !!

๋ ˆ์ด์ €๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ ์—ด๋ฆผ ๊ด„ํ˜ธ ๊ฐœ์ˆ˜๋ฅผ ๋”ํ•˜๊ณ , ๋‹ซํž˜ ๊ด„ํ˜ธ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ 1 ์„ ๋”ํ•ด์ฃผ๋ฉด ๋ !!

def solution(arr):
	arr = arr.replace('()', 'L')
	answer = 0
	open = list()

	for val in arr:
		if val == '(':
			open.append(val)
		elif val == ')':
			answer += 1
			open.pop()
		elif val == 'L':
			answer += len(open)

	return answer

๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ณธ๋‹ค๋ฉด ๋ฐ”๋กœ ์ดํ•ด๊ฐ€ ๊ฐˆ ๊ฒƒ์ด๋‹ค.


์ด ๋ฌธ์ œ๋Š” ๋‹ค๋ฅธ์‚ฌ๋žŒํ’€์ด ์ค‘ ์ •๋ง ์ž˜ ํ’€์—ˆ๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“œ๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค !!

def solution(arrangement):
    answer = 0
    sticks = 0
    rasor_to_zero = arrangement.replace('()','0')

    for i in rasor_to_zero:
        if i == '(':
            sticks += 1
        elif i =='0' :
            answer += sticks
        else :
            sticks -= 1
            answer += 1

    return answer
#---------------------------------------------------------#
  def solution(arrangement):
    answer = 0
    stack = 0
    laseron = False
    for p in arrangement:
        if p == '(':
            laseron = True
            stack += 1
        else:
            if laseron==True:
                answer += stack-1
                laseron=False
            else:
                answer += 1
            stack -= 1

    return answer

์ด ๋‘ ๊ฐ€์ง€ ์ธ๋ฐ, ์ฒซ ๋ฒˆ์งธ ์ฝ”๋“œ๋Š” ๋‚˜์™€ ํ’€์ด๋ฐฉ์‹์€ ๊ฐ™์€๋ฐ ์Šคํƒ์—†์ด ์ˆซ์ž๋กœ๋งŒ ํ’€์—ˆ๋‹ค.

append, pop ์„ +, - ๋กœ ํ–ˆ๋Š”๋ฐ ๊น”๋”..

๋‘ ๋ฒˆ์งธ๋Š” replace ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ‘ผ ๋ฐฉ์‹์ด๋‹ค.

โ€™(โ€˜ ๊ฐ€ ์˜ค๋ฉด ๋ ˆ์ด์ €๋ฅผ ์ผœ๊ณ  โ€˜)โ€™ ๊ฐ€ ์™”์„๋•Œ ๋ ˆ์ด์ €๊ฐ€ ์ผœ์ ธ์žˆ์œผ๋ฉด ๋ ˆ์ด์ €, ๊บผ์ ธ์žˆ์œผ๋ฉด ๋ง‰๋Œ€๊ธฐ์˜ ๋์ด๋‹ค.


Categories:

Updated:

Leave a comment