BOJ/백준-1439-python

cosmos·2021년 6월 17일
1
post-thumbnail

문제📖

풀이🙏

  • 첫째 줄에 문자열 S가 주어진다.
  • 다솜이가 할 수 있는 해옹은 S에서 연속된 하나 이상의 숫자를 잡고 모두 뒤집는 것이다.
  • 첫째 줄에 다솜이가 해야하는 행동의 최소 횟수를 출력하라.

코드💻

# boj, 1439 : 뒤집기, python
# 그리디 알고리즘
def solution(word):
    result = [0,0]
    
    if word[0] == '0':
        result[0] += 1
    else:
        result[1] += 1
        
    for i in range(len(word)-1):
        if word[i] != word[i+1]:
            if word[i+1] == '0':
                result[0] += 1
            else:
                result[1] += 1

    return min(result)

S = list(str(input()))

print(solution(S))

결과😎

출처 && 깃허브📝

boj
github

0개의 댓글