강의

멘토링

커뮤니티

Inflearn Community Q&A

phillnfeel1306's profile image
phillnfeel1306

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

2. Iron bars (stack)

코딩테스트에서 for문과 if문으로만 이루어진 코드보다 스택같은 자료 구조를 쓰면 더 점수를 받는다거나 그런게 있을까요?

Written on

·

266

0

코딩테스트에서 for문과 if문으로만 이루어진 코드보다 스택같은 자료 구조를 쓰면 더 점수를 받는다거나 그런게 있을까요?

일단 어떠한 방식으로던 답을 맞추는게 최우선이겠죠?

강의를 보기전에 나름의 논리를 세워서 풀었는데 테스트 케이스를 다 통과해서 저런식으로 경우를 나열해서 짜면 뭔가 빈약하지 않을까 생각이 들어 여쭤봅니다.

import sys
sys.stdin=open("input.txt", "rt")

s=input()
cnt=0 #잘려진 총 조각 수
box=0 #레이저를 맞게 되면 잘라지는 조각의 수

for i in range(len(s)):
    if s[i]=='(' and s[i+1]==')':
        cnt+=box
    elif s[i]=='(' and s[i+1]=='(':
        box+=1
    elif s[i]==')' and s[i-1]==')':
        box-=1
        cnt+=1
        
print(cnt)
코테 준비 같이 해요! python

Answer 1

1

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

문제를 맞추는게 중요하죠. 뭘로 풀든 통과되는게 중요하죠. 잘하신 코드입니다.

phillnfeel1306's profile image
phillnfeel1306

asked

Ask a question