题目描述:
在一场经典的德州扑克游戏中,有一种牌型叫做“葫芦”。“葫芦”由五张牌组成,其中包括三张相同牌面值的牌 和另外两张相同牌面值的牌 。如果两个人同时拥有“葫芦”,我们会优先比较牌 的大小,若牌 相同则再比较牌 的大小。
在这个问题中,我们对“葫芦”增加了一个限制:组成“葫芦”的五张牌牌面值之和不能超过给定的最大值 。牌面值的大小规则为:A > K > Q > J > 10 > 9 > ... > 2,其中 A 的牌面值为1,K 为13,依此类推。
给定一组牌,你需要找到符合规则的最大的“葫芦”组合,并输出其中三张相同的牌面和两张相同的牌面。如果找不到符合条件的“葫芦”,则输出 “0, 0”。
def solution(n, max1, array):
# 统计每种牌面值的出现次数
count = {}
for card in array:
if card in count:
count[card] += 1
else:
count[card] = 1
# 对牌面值进行排序
sorted_cards = sorted(count.keys(), reverse=True)
# 将A放在最前面
if 1 in sorted_cards:
sorted_cards.remove(1)
sorted_cards.insert(0, 1)
# 寻找符合条件的“葫芦”
for a in sorted_cards:
if count[a] >= 3:
for b in sorted_cards:
if b != a and count[b] >= 2:
equation = a * 3 + b * 2
if equation <= max1:
return [a, b]
return [0, 0]