leetcode 6096. 咒语和药水的成功对数(java)

80场双周赛

第二题中等题

class Solution {
    public int[] successfulPairs(int[] sp, int[] po, long su) {
        int n = sp.length;
        int m = po.length;
        int[][] a = new int[n][2];
        for (int i = 0; i < n; i++) {
            a[i][0] = sp[i];
            a[i][1] = i;
        }
        Arrays.sort(a, (o1, o2) -> o1[0] - o2[0]);
        Integer[] p = new Integer[m];
        for (int i = 0; i y - x);
        int[] res = new int[n];
        int j = -1;
        for (int i = 0; i < n; i++) {
            while(j + 1 < m && (long)a[i][0] * p[j + 1] >= su) j++;
            res[a[i][1]] = j + 1;
        }
        return res;
    }
}

感觉排序+二分也可以 但是没做出来

你可能感兴趣的:(学习做题leetcode,leetcode,算法,蓝桥杯,周赛)