백준 1439번 뒤집기

highway92·2021년 10월 12일

백준

목록 보기
23/27

문제출처 : https://www.acmicpc.net/problem/1439

풀이과정

  1. for 문을 돌면서 연속된 0의 인덱스는 zero에 연속된 1의 인덱스는 one에 넣는다.

  2. for문의 마지막을 주의해야 한다.

S=input()
zero=[]
one=[]
start = 0
end = None
for i in range(len(S)): 
    if i>=1 and S[i] != S[i-1]:
        end = i
        if S[i] == "1":
            zero.append([start,end])
        else:
            one.append([start,end])
        start = i
    
    if i == len(S)-1:
        if S[i] == "1":
            one.append([start,len(S)])
        else:
            zero.append([start,len(S)])

print(min(len(zero),len(one)))
profile
웹 개발자로 활동하고 있습니다.

0개의 댓글