처음을 누르고 시작하는 방법과 그렇지 않은 방법 두 가지 중 유효한 최솟값으로 간다.
import sys
input = sys.stdin.readline
def change(now, cnt): 
    if cnt == 1: 
        now[0] ^= 1
        now[1] ^= 1
    for i in range(1, n-1): 
        if now[i-1] != target[i-1]: 
            cnt += 1 
            now[i-1] ^= 1
            now[i] ^= 1 
            now[i+1] ^= 1 
    if now[n-2] != target[n-2]:
        cnt += 1 
        now[n-2] ^= 1
        now[n-1] ^= 1
    return cnt if now == target else float('inf')
n = int(input()) 
now = list(map(int, input().rstrip())) 
target = list(map(int,input().rstrip())) 
res1 = change(now[:], 0) 
res2 = change(now[:], 1) 
print(min(res1, res2) if res1 != float('inf') or res2 != float('inf') else -1)'practivceAlgorithm > 백준' 카테고리의 다른 글
| [백준][Python] 5671 호텔 방 번호 (0) | 2021.09.13 | 
|---|---|
| [백준][Python] 2143 두 배열의 합 (0) | 2021.09.13 | 
| [백준][Python] 1965 상자넣기 (0) | 2021.09.12 | 
| [백준][Python] 11728 배열합치기 (0) | 2021.09.12 | 
| [백준][Python] 1637 날카로운 눈 (0) | 2021.09.11 |