LeetCode 870. 优势洗牌


LeetCode 870. 优势洗牌

给定两个大小相等的数组 A 和 B,A 相对于 B 的优势可以用满足 A[i] > B[i] 的索引 i 的数目来描述。
返回 A 的任意排列,使其相对于 B 的优势最大化。

示例 1:
输入:A = [2,7,11,15], B = [1,10,4,11]
输出:[2,11,7,15]

示例 2:
输入:A = [12,24,8,32], B = [13,25,32,11]
输出:[24,32,8,12]

提示:
1 <= A.length = B.length <= 10000
0 <= A[i] <= 10^9
0 <= B[i] <= 10^9

思路 : 田忌赛马
排序俩个数组,然后比较
记得使用map记录排序前的位置信息

    public int[] advantageCount(int[] A, int[] B) {
        if(A.length!=B.length){
            return new int[0];
        }
        Map> map = new HashMap<>();
        for(int x=0;x());
            }
            map.get(B[x]).add(x);
        }

        Arrays.sort(A);
        Arrays.sort(B);

        int[] answer = new int[A.length];

        int indexA = 0;
        int indexB = 0;
        int pos = 0;
        int last = A.length-1;

        while(indexAB[indexB]){
                answer[pos++] = A[indexA++];
                indexB++;
            }else{
                answer[last--] = A[indexA++];
            }
        }

        int[] result = new int[answer.length];

        for(int x=0;x

总结

*仔细观察 找到规律

反馈与建议

你可能感兴趣的:(LeetCode 870. 优势洗牌)