IT/coding study

[acmicpc] 17615. 볼 모으기(python)

seyeonHello 2022. 8. 22. 23:00
import sys
input=sys.stdin.readline

N=int(input())
balls=input().strip()
def find_index(color): # 맨 우측과 좌측 중, 볼이 많이 모여있는 곳 찾기
    left, right = 0, 0
    for i in range(N):
        if balls[i] != color: break
        left += 1
    for i in range(N - 1, -1, -1):
        if balls[i] != color: break
        right += 1
    if left<right:
        return find_move(False, right, color) #맨 우측
    else:
        return find_move(True, left, color) #맨 좌측
        
def find_move(loc, cnt, color): # 옮길 볼의 개수 구하기
    if loc:
        return balls[cnt:].count(color)
    else:
        return balls[:N - cnt].count(color)

red=find_index('R')
blue=find_index('B')
print(red if red<blue else blue)

 


 

https://www.acmicpc.net/problem/17615

 

17615번: 볼 모으기

첫 번째 줄에는 볼의 총 개수 N이 주어진다. (1 ≤ N ≤ 500,000) 다음 줄에는 볼의 색깔을 나타내는 문자 R(빨간색 볼) 또는 B(파란색 볼)가 공백 없이 주어진다. 문자열에는 R 또는 B 중 한 종류만 주

www.acmicpc.net