华为OD机试真题 - 连续出牌数量

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] nums = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        String[] c = in.nextLine().split(" ");
        List cards = new ArrayList();
        for (int i = 0; i < nums.length; i++) {
            cards.add(new Card(nums[i], c[i].charAt(0)));
        }
        System.out.println(getResult(cards));
    }

    public static int getResult(List<Card> cards) {
        Queue<Integer> path = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int i = 0; i < cards.size(); i++) {
            List<Card> temp = cards.stream().collect(Collectors.toList());
            temp.remove(i);
            DFS(temp, path,cards.get(i), 1);
        }
        if (path.size() > 0)
            return path.poll();
        return 0;
    }

    public static void DFS(List<Card> totol, Queue<Integer> path, Card cur, int depth) {
        boolean has = false;
        for (int i = 0; i < totol.size(); i++) {
            Card temp = totol.get(i);
            if (temp.num == cur.num || temp.color == cur.color) {
                List<Card> copy = totol.stream().collect(Collectors.toList());
                has = true;
                copy.remove(i);
                DFS(copy, path, totol.get(i), depth + 1);
            }
        }
        if (!has) {
            path.add(depth);
        }
    }

    static class Card {
        public int num;
        public char color;

        public Card(int num, char color) {
            this.num = num;
            this.color = color;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj instanceof Card)
                return false;
            Card c = (Card) obj;
            if (this.num == c.num &amp;&amp; this.color == c.color)
                return true;
            return false;
        }
    }
全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 20:55
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务