Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Created June 6, 2020 16:11
Show Gist options
  • Save DongguemYoo/ca4cd127b4ffab8e041712e96ecd012b to your computer and use it in GitHub Desktop.
Save DongguemYoo/ca4cd127b4ffab8e041712e96ecd012b to your computer and use it in GitHub Desktop.
[코딩테스트] 스택/큐 쇠막대기.python
//c++로 풀엇던 것을 다시 푼것이라...그냥 기억에 남아서 이렇게 풀어버린듯
//어쨋든 잘 해결했다!
def solution(arrangement):
answer = 0
isLaser = False
stick = []
for x in arrangement:
if x == '(':
stick.append(x)
isLaser = True
else:
if isLaser:
stick.pop()
answer+=len(stick)
isLaser = False
else:
stick.pop()
answer+=1
return answer
//다른사람 풀이
//와 미친...레이져를 R로 표현을 해버리네
//이게 제일 직관적이고 지렷다...
def solution(arrangement):
count = 0
stack = []
arrangement = arrangement.replace("()", "R");
for c in arrangement:
if c == "(":
stack.append(c)
if c == "R":
count += len(stack)
if c == ")":
stack.pop()
count += 1
return count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment