-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17615.py
More file actions
53 lines (45 loc) · 1.3 KB
/
17615.py
File metadata and controls
53 lines (45 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
n = int(input())
balls = [''] * n
row = input()
for i in range(n):
balls[i] = row[i]
ball_groups = []
cur_color = balls[0]
cur_cnt = 1
for i in range(1, n):
if balls[i] == cur_color:
cur_cnt += 1
else:
ball_groups.append((cur_color, cur_cnt))
cur_color = balls[i]
cur_cnt = 1
ball_groups.append((cur_color, cur_cnt))
if len(ball_groups) <= 2:
print(0)
exit()
answer = 987654321
# 가장 왼쪽의 공에 있는 색갈과 같은 공들을 모두 왼쪽으로
cur_answer = 0
for i in range(2, len(ball_groups), 2):
color, cnt = ball_groups[i]
cur_answer += cnt
answer = min(answer, cur_answer)
# 가장 오른쪽에 있는 색갈과 같은 공들을 모두 오른쪽으로
cur_answer = 0
for i in range(len(ball_groups) - 3, -1, -2):
color, cnt = ball_groups[i]
cur_answer += cnt
answer = min(answer, cur_answer)
# 왼쪽에서 두 번째 그룹인 공들을 모두 왼쪽으로
cur_answer = 0
for i in range(1, len(ball_groups), 2):
color, cnt = ball_groups[i]
cur_answer += cnt
answer = min(answer, cur_answer)
# 오른쪽에서 두 번째 그룹인 공들을 모두 오른쪽으로
cur_answer = 0
for i in range(len(ball_groups) - 2, -1, -2):
color, cnt = ball_groups[i]
cur_answer += cnt
answer = min(answer, cur_answer)
print(answer)