算法 求最大葫芦数

问题描述

德州扑克是一种经典的扑克游戏,里面有一种牌型叫“葫芦”。
“葫芦”由五张牌组成,其中包含三张一样的牌a和另外两张一样的牌b。
“葫芦”跟“葫芦”之间可以比较大小,先比较a的大小,如果a一样大再比较b的大小。
德州扑克不包含大小王,只包含常规的52张牌。在13种不同的牌面里面,大小规则是A > K > Q > J > 10 > 9 > … > 2。也就是说,A最大,K次之,2最小。
这里给“葫芦”加一个限制规则,“葫芦”的牌面值之和不能超过max(可以等于),A,2,3,…,J,Q,K的牌面值分别是1到13。

问题

给出n张牌,从里面寻找符合规则的最大葫芦组合

输入格式

第一行输入两个整数,代表牌的数量n和牌面值之和的限制max
第二行输入n个整数,代表n张牌的牌面,其中数字1代表牌面A,数字2到10分别代表牌面2到10,11代表牌面J,12代表牌面Q,13代码牌面K。

输出格式

最大的葫芦组合里面,三张的牌面和两张的牌面,如果没有葫芦则输出“0,0”

输入样式

样例1

9 34
6 6 6 8 8 8 5 5 1

样例2

9 37
9 9 9 9 6 6 6 6 13

样例3

9 40
1 11 13 12 7 8 11 5 6

输出样式

样例1

8 6

样例2

9 6

样例3

0 0

数据范围

牌数n的范围:5 < n < 40,0 < max < 100
牌面的范围:1 ≤ v ≤ 13
跟现实中扑克一样,每种牌面最多4张

答案

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class Main {
    public static int[] solution(int n, int max, int[] array) {
        // Edit your code here
        HashMap<Integer, Integer> checkMap = new HashMap<>();
        for(Integer i : array){
            if (!checkMap.containsKey(i)){
                checkMap.put(i,1);
            }else {
                int a = checkMap.get(i);
                checkMap.put(i,a+1);
            }
        }

        ArrayList<Integer> ThreeList = new ArrayList<>();
        ArrayList<Integer> TwoList = new ArrayList<>();

        checkMap.forEach((key,value)->{
            if (value==3){
                ThreeList.add(key);
                TwoList.add(key);
            }
            if (value==2){
                TwoList.add(key);
            }
            if (value==4){
                ThreeList.add(key);
                TwoList.add(key);
            }
        });

        Collections.sort(ThreeList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return 01-o2;
            }
        });
        Collections.sort(TwoList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return 01 - o2;
            }
        });
        if (ThreeList.size() == 0){
            return new int[]{0, 0};
        }

        int threeRes = ThreeList.get(0);
        int index = 0;
        for (int i = 0; i < TwoList.size(); i++) {
            if (threeRes == TwoList.get(i)){
                index = i;
            }
        }
        int twoRes = TwoList.get(index+1);



        return new int[]{threeRes, twoRes};
    }

    public static void main(String[] args) {
        // Add your test cases here

        System.out.println(java.util.Arrays.equals(solution(9, 34, new int[]{6, 6, 6, 8, 8, 8, 5, 5, 1}), new int[]{8, 6}));
        System.out.println(java.util.Arrays.equals(solution(9, 37, new int[]{9, 9, 9, 9, 6, 6, 6, 6, 13}), new int[]{9,6}));
        System.out.println(java.util.Arrays.equals(solution(9, 40, new int[]{1, 11, 13, 12, 7, 8, 11, 5, 6}), new int[]{0, 0}));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攒了一袋星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值