思路:
贪心贪心贪心!
每次选剩余最长的,不满足的话那就是次长的!
src:
class Solution:
def longestDiverseString(self, a: int, b: int, c: int) -> str:
# 贪心,每次都选剩下最多的
# 若不符合,就选次多的
ans = ""
cnt = [[a, 'a'], [b, 'b'], [c, 'c']]
while True:
# 选出最多的
cnt.sort(key = lambda x: -x[0])
hasNext = False
for i, (num, ch) in enumerate(cnt):
# 如果没了
if num <= 0:
continue
# 如果重复三次
if len(ans) >= 2 and ans[-1] == ch and ans[-2] == ch:
continue
# 可以加了
hasNext = True
ans += ch
# 更新
cnt[i][0] -= 1
# 记得break
break
# 如果加不了了
if not hasNext:
return ans
总结:
一定要贪心哦



