刚刚哪位回复的第一题贪心算法(咋删掉了orz
思路是对的,最后细节差一点,需要判断最后一位和最后大小写状态是否相同,不同则需要再加1。其他都没问题,我本地测了一下,100个case都过了。
认领一下这是谁的代码😂我刚刚复制下来测试了:
def get_min_click(text):
n = len(text)
state = False #caps state initial is False
caps = 0
shifts = 0
for i in range(n-1):
char = text[i]
char_next = text[i+1]
if char.isupper() is not state and char_next.isupper() is char.isupper():
state = not state #state update
caps += 1 #record 1 time caps click
elif char.isupper() is not state and char_next.isupper() is not char.isupper():
shifts += 1 #record 1 time shift click
return n+caps+shifts+(text[-1].isupper() != state)