1.题目
问题描述
在一场经典的德州扑克游戏中,有一种牌型叫做“葫芦”。“葫芦”由五张牌组成,其中包括三张相同牌面值的牌 aa 和另外两张相同牌面值的牌 bb。如果两个人同时拥有“葫芦”,我们会优先比较牌 aa 的大小,若牌 aa 相同则再比较牌 bb 的大小。
在这个问题中,我们对“葫芦”增加了一个限制:组成“葫芦”的五张牌牌面值之和不能超过给定的最大值 maxmax。牌面值的大小规则为:A > K > Q > J > 10 > 9 > ... > 2,其中 A 的牌面值为1,K 为13,依此类推。
给定一组牌,你需要找到符合规则的最大的“葫芦”组合,并输出其中三张相同的牌面和两张相同的牌面。如果找不到符合条件的“葫芦”,则输出 “0, 0”。
测试样例
样例1:
输入:n = 9, max = 34, array = [6, 6, 6, 8, 8, 8, 5, 5, 1]
输出:
[8, 5]
样例2:
输入:n = 9, max = 37, array = [9, 9, 9, 9, 6, 6, 6, 6, 13]
输出:
[6, 9]
样例3:
输入:n = 9, max = 40, array = [1, 11, 13, 12, 7, 8, 11, 5, 6]
输出:
[0, 0]
2.思路
先对每个出现的数进行计数,然后按照A > K > Q > J > 10 > 9 > ... > 2的顺序先找最大的三张,再找和它不一样的最大的两张。
3.代码
#include <functional>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
std::vector<int> solution(int n, int max, const std::vector<int>& array) {
// Edit your code here
// 定义map记录数组中每个数出现的次数
// std::map 默认是按键的升序排列的。想要按键的降序排列,可以使用 std::map 的第三个模板参数来指定比较函数为 std::greater<int>
map<int, int, greater<int>> countMap;
for(int num : array){
countMap[num] ++;
}
// 先找三张的牌a,再找两张的牌b
for (const auto& pair : countMap){
if (pair.second >= 3){
for (const auto& pair2 : countMap){
// 注意还有最大值的限制
if (pair2.first != pair.first and pair2.second >= 2 and 3 * pair.first + 2 * pair2.first <= max){
return {pair.first, pair2.first};
}
}
}
return {0, 0};
}
}
int main() {
// Add your test cases here
// std::vector<int> result1 = solution(9, 34, {6, 6, 6, 8, 8, 8, 5, 5, 1});
// std::cout << (result1 == std::vector<int>{8, 5}) << std::endl;
std::vector<int> result2 = solution(9, 37, {9, 9, 9, 9, 6, 6, 6, 6, 13});
std::cout << (result2 == std::vector<int>{6, 9}) << std::endl;
for (int num : result2) {
std::cout << num << " ";
}
// std::vector<int> result3 = solution(9, 40, {1, 11, 13, 12, 7, 8, 11, 5, 6});
// std::cout << (result3 == std::vector<int>{0, 0}) << std::endl;
return 0;
}
开始误把return {0, 0};
写在第一个for
循环内,导致如果第一个数不符合要求,直接就返回{0,0}
了,样例二无法通过。
修改后,第四个样例无法通过:
输入
n= 31
max = 42
array = [3,3,11,12,12,2,13,5,13,1,13,8,8,1,8,13,12,9,2,11,3,5,8,11,1,11,1,5,4,2,5]
输出
你的输出 =[13,1]
预期输出=[1,13]
原因在于没有注意到1是最大的,要按照A > K > Q > J > 10 > 9 > ... > 2的顺序。
#include <functional>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
std::vector<int> solution(int n, int max, const std::vector<int>& array) {
// Edit your code here
// 定义map记录数组中每个数出现的次数
// std::map 默认是按键的升序排列的。想要按键的降序排列,可以使用 std::map 的第三个模板参数来指定比较函数为 std::greater<int>
map<int, int> countMap;
for(int num : array){
countMap[num] ++;
}
vector<int> vals = {1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
// 先找三张的牌a,再找两张的牌b
for (int a : vals){
if (countMap[a] >= 3){
for (int b : vals){
if(3 * a + 2 * b <= max && countMap[b] >= 2 && a != b){
return {a, b};
}
}
}
}
return {0, 0};
}
int main() {
// Add your test cases here
std::vector<int> result1 = solution(9, 34, {6, 6, 6, 8, 8, 8, 5, 5, 1});
std::cout << (result1 == std::vector<int>{8, 5}) << std::endl;
std::vector<int> result2 = solution(9, 37, {9, 9, 9, 9, 6, 6, 6, 6, 13});
std::cout << (result2 == std::vector<int>{6, 9}) << std::endl;
// for (int num : result2) {
// std::cout << num << " ";
// }
std::vector<int> result3 = solution(9, 40, {1, 11, 13, 12, 7, 8, 11, 5, 6});
std::cout << (result3 == std::vector<int>{0, 0}) << std::endl;
return 0;
}