DHistory

[Baekjoon] Greedy - 20365 블로그 2 (생각노트) 본문

Computer Science/Algorithm

[Baekjoon] Greedy - 20365 블로그 2 (생각노트)

ddu0422 2023. 8. 24. 13:26

문제

 

20365번: 블로그2

neighbor 블로그를 운영하는 일우는 매일 아침 풀고 싶은 문제를 미리 정해놓고 글을 올린다. 그리고 매일 밤 각각의 문제에 대하여, 해결한 경우 파란색, 해결하지 못한 경우 빨간색으로 칠한

www.acmicpc.net

 

풀이

import re

n = int(input())
text = input()


def solution(text):
    # 연속으로 나온 문자는 하나의 문자로 변경한다.
    for value in ['B', 'R']:
        standard = '(' + value + '){2,}'
        text = re.sub(r'{}'.format(standard), value, text)
    
    # 둘 중 많은 색으로 칠하기(1) + 나머지 칠하기
    return 1 + min(text.count('B'), text.count('R'))


print(solution(text))

 

채점 결과

 

 

참고해야할 풀이

n = int(input())
text = input()


def solution_refactor(text):
    # 연속된 문자열은 하나의 원소로 이루어진다.
    r = text.split('B')
    b = text.split('R')
    
    return 1 + min(len(r) - r.count(''), len(b) - b.count(''))


print(solution_refactor(text))