牛客网编程题——第4节 课后作业1-调整队形

牛客网编程题——第4节 课后作业1-调整队形_第1张图片

可能的结果只有两种
即 所有G在左侧
或所有B在左侧

alist = [a for a in input()]
t1 = 0
t2 = 0

index = 0
lastGindex = 0
while lastGindex <= len(alist) - 1 and 'G' in alist[lastGindex:]:
    G_index = alist.index('G')
    alist[G_index] ='g'
    t1 += G_index - index
    index += 1
    lastGindex = G_index+1
index = 0
lastBindex = 0
while lastBindex <= len(alist) - 1 and 'B' in alist[lastBindex:]:
    B_index = alist.index('B')
    alist[B_index] ='b'
    t2 += B_index - index
    index += 1
    lastBindex = B_index+1
print(min(t1,t2))

你可能感兴趣的:(牛客网,python编程技巧)