2347. 最好的扑克手牌
2347. 最好的扑克手牌
题目
You are given an integer array ranks
and a character array suits
. You have 5
cards where the ith
card has a rank of ranks[i]
and a suit of suits[i]
.
The following are the types of poker hands you can make from best to worst:
"Flush"
: Five cards of the same suit."Three of a Kind"
: Three cards of the same rank."Pair"
: Two cards of the same rank."High Card"
: Any single card.
Return a string representing the best type of poker hand you can make with the given cards.
Note that the return values are case-sensitive.
Example 1:
Input: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
Output: "Flush"
Explanation: The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
Example 2:
Input: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
Output: "Three of a Kind"
Explanation: The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind".
Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
Also note that other cards could be used to make the "Three of a Kind" hand.
Example 3:
Input: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
Output: "Pair"
Explanation: The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
Note that we cannot make a "Flush" or a "Three of a Kind".
Constraints:
ranks.length == suits.length == 5
1 <= ranks[i] <= 13
'a' <= suits[i] <= 'd'
- No two cards have the same rank and suit.
题目大意
给你一个整数数组 ranks
和一个字符数组 suit
。你有 5
张扑克牌,第 i
张牌大小为 ranks[i]
,花色为 suits[i]
。
下述是从好到坏你可能持有的 手牌类型:
"Flush"
:同花,五张相同花色的扑克牌。"Three of a Kind"
:三条,有 3 张大小相同的扑克牌。"Pair"
:对子,两张大小一样的扑克牌。"High Card"
:高牌,五张大小互不相同的扑克牌。
请你返回一个字符串,表示给定的 5 张牌中,你能组成的 最好手牌类型 。
注意: 返回的字符串 大小写 需与题目描述相同。
示例 1:
输入: ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
输出: "Flush"
解释: 5 张扑克牌的花色相同,所以返回 "Flush" 。
示例 2:
输入: ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
输出: "Three of a Kind"
解释: 第一、二和四张牌组成三张相同大小的扑克牌,所以得到 "Three of a Kind" 。
注意我们也可以得到 "Pair" ,但是 "Three of a Kind" 是更好的手牌类型。
有其他的 3 张牌也可以组成 "Three of a Kind" 手牌类型。
示例 3:
输入: ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
输出: "Pair"
解释: 第一和第二张牌大小相同,所以得到 "Pair" 。
我们无法得到 "Flush" 或者 "Three of a Kind" 。
提示:
ranks.length == suits.length == 5
1 <= ranks[i] <= 13
'a' <= suits[i] <= 'd'
- 任意两张扑克牌不会同时有相同的大小和花色。
解题思路
判断是否是 Flush:
- 使用
Set
统计suits
中的花色种类数量。如果Set
的大小为1
,说明所有花色相同,直接返回"Flush"
。
- 使用
统计点数频率:
- 使用
Map
统计ranks
中每种点数出现的次数。 - 计算点数的最大频率
maxFreq
,用于判断其他手牌类型。
- 使用
判断手牌类型:
- 如果
maxFreq >= 3
,说明存在至少三张点数相同的牌,返回"Three of a Kind"
。 - 如果
maxFreq == 2
,说明存在两张点数相同的牌,返回"Pair"
。 - 如果
maxFreq == 1
,说明没有任何点数重复,返回"High Card"
。
- 如果
复杂度分析
时间复杂度:
O(1)
- 计算花色种类:
O(5)
,因为suits
的长度为 5。 - 统计点数频率:
O(5)
,因为ranks
的长度为 5。 - 计算最大频率:
O(5)
,因为ranksFreq.values()
的长度不会超过 5。
- 计算花色种类:
空间复杂度:
O(1)
,使用了Set
和Map
,大小为O(5) = O(1)
。
代码
/**
* @param {number[]} ranks
* @param {character[]} suits
* @return {string}
*/
var bestHand = function (ranks, suits) {
let suitsSet = new Set(suits);
if (suitsSet.size == 1) return 'Flush';
let ranksFreq = new Map();
for (let num of ranks) {
ranksFreq.set(num, (ranksFreq.get(num) || 0) + 1);
}
const maxFreq = Math.max(...ranksFreq.values());
if (maxFreq >= 3) return 'Three of a Kind';
if (maxFreq == 2) return 'Pair';
return 'High Card';
};
相关题目
题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
---|---|---|---|---|---|
2525 | 根据规则将箱子分类 | 数学 | 🟢 | 🀄️ 🔗 |