只能说算法做题全凭运气

问题描述

在一款多人游戏中,每局比赛需要多个玩家参与。如果发现两名玩家至少一起玩过两局比赛,则可以认为这两名玩家互为队友。现在你有一份玩家(通过玩家ID标识)和比赛局次(通过比赛ID标识)的历史记录表,目标是帮助某位指定玩家找到所有符合条件的队友。

例如样例1,已知以下比赛历史记录:

玩家ID 游戏ID
1 1
1 2
1 3
2 1
2 4
3 2
4 1
4 2
5 2
5 3

我们需要帮助ID为1的玩家找到所有至少与其一起玩过两次比赛的队友。

输入:

  • id:指定的玩家ID
  • num:比赛历史记录的条目数
  • array:比赛历史记录,每个条目包含一个数组 [玩家ID, 游戏ID]

返回规则如下:

  • 返回指定玩家的所有队友ID,以升序输出。如果没有队友,则输出空列表。

测试样例

样例1:

输入:id = 1, num = 10, array = [[1,1], [1,2], [1,3], [2,1], [2,4], [3,2], [4,1], [4,2], [5,2], [5,3]]
输出:[4, 5]

样例2:

输入:id = 2, num = 6, array = [[2,1], [2,3], [1,1], [1,2], [3,1], [4,3]]
输出:[]

样例3:

输入:id = 3, num = 8, array = [[3,1], [3,2], [3,3], [4,1], [5,2], [6,3], [7,1], [7,2]]
输出:[7]

我这将近一个小时的题解:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        // Add your test cases here
        System.out.println(
                Arrays.equals(
                        solution(1, 10,
                                new int[][] {
                                        { 1, 1 }, { 1, 2 }, { 1, 3 }, { 2, 1 }, { 2, 4 }, { 3, 2 },
                                        { 4, 1 }, { 4, 2 }, { 5, 2 }, { 5, 3 }
                                }),
                        new int[] { 4, 5 }));
    }

    public static int[] solution(int id, int num, int[][] array) {
        List list = new ArrayList<>();


        Arrays.sort(array, new Comparator() {
            @Override
            public int compare(int[] a, int[] b) {
                // 先按第一个元素升序排序
                int cmp = Integer.compare(a[0], b[0]);
                if (cmp != 0) {
                    return cmp;
                }
                // 如果第一个元素相同,再按第二个元素升序排序
                return Integer.compare(a[1], b[1]);
            }
        });

        // 目标的比赛的所有ID
        HashSet set = new HashSet<>();
        for (int i = 0; i < array.length; i++) {
            if (array[i][0] == id) {
                set.add(array[i][1]);
            }
        }

        // 一遍遍历实现,首先记录当前的对象
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            //跳过自己的id
            while (i != array.length - 1 && array[i][0] == id) {
                i++;
            }

            // 建立锚点,对当前的区域进行扫描
            int pre = array[i][0];
            int j = i;
            while (j < array.length && array[j][0] == pre) {
                if (set.contains(array[j][1])) {
                    count++;
                }
                j++;
            }

            if (count >= 2) {
                list.add(array[j - 1][0]);
            }
            count = 0;

            // 判断边界
            if (j == array.length) {
                break;
            }
            i = j - 1;
        }
        int[] result = new int[list.size()];
        for (int i = 0; i < result.length; i++) {
            result[i] = list.get(i);
        }

        return result;
    }
}

 这是一个简单题目啊啊啊啊啊啊!有时候感觉做算法题真的很想暴走!!!!!!

还是写下思路,首先将这个二维数组进行排序,然后直接一遍遍历,统计每一个id中和目标对象出现的场次一样的次数,大于两局就加入列表,结束。

你可能感兴趣的:(算法,java,开发语言)