LeetCode(力扣) 315. 计算右侧小于当前元素的个数

https://leetcode-cn.com/problems/count-of-smaller-numbers-after-self/

难度困难

给你一个整数数组 nums ,按要求返回一个新数组 counts 。数组 counts 有该性质: counts[i] 的值是  nums[i] 右侧小于 nums[i] 的元素的数量。

示例 1:

输入:nums = [5,2,6,1]
输出:[2,1,1,0] 
解释:
5 的右侧有 2 个更小的元素 (2 和 1)
2 的右侧仅有 1 个更小的元素 (1)
6 的右侧有 1 个更小的元素 (1)
1 的右侧有 0 个更小的元素


示例 2:

输入:nums = [-1]
输出:[0]


示例 3:

输入:nums = [-1,-1]
输出:[0,0]
 

提示:

1 <= nums.length <= 10^{5}
- 10^{5} <= nums[i] <= 10^{5}

 

 方法一:树状数组

    public static List<Integer> countSmaller(int[] nums) {
        int len = nums.length;
        List<Integer> counts = new ArrayList<>(len);
        //去重 离散化
        Set<Integer> set = new TreeSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int index = 1;
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : set) {
            map.put(num, index++);
        }
        //BIT
        int[] tree = new int[index + 1];
        //边查询边更新
        for (int i = len - 1; i >= 0; i--) {
            int num = nums[i];
            int x = map.get(num);
            int count = query(tree, x - 1);
            counts.add(count);
            update(tree, x);
        }

        int left = 0;
        int right = len - 1;
        while (left < right) {
            int temp = counts.get(left);
            counts.set(left, counts.get(right));
            counts.set(right, temp);
            left++;
            right--;
        }
        return counts;
    }

    private static void update(int[] tree, int index) {
        while (index < tree.length) {
            tree[index] += 1;
            index += (index & - index);
        }
    }

    private static int query(int[] tree, int index) {
        int res = 0;
        while (index > 0) {
            res += tree[index];
            index -= (index & - index);
        }
        return res;
    }

 

方法二:归并排序

    public static List<Integer> countSmaller(int[] nums) {
        int len = nums.length;
        int[] indexs = new int[len];
        for (int i = 0; i < len; i++) {
            indexs[i] = i;
        }
        int[] counts = new int[len];
        int[] temp = new int[len];
        mergeAndCount(nums, indexs, counts, temp, 0, len - 1);

        List<Integer> list = new ArrayList<>();
        for (int num : counts) {
            list.add(num);
        }
        return list;
    }

    private static void mergeAndCount(int[] nums, int[] indexs, int[] counts, int[] temp, int left, int right) {
        if (left == right) return;
        int mid = left + (right - left) / 2;
        mergeAndCount(nums, indexs, counts, temp, left,  mid);
        mergeAndCount(nums, indexs, counts, temp, mid + 1,  right);

        int leftPtr = left;
        int rightPtr = mid + 1;
        int pointer = left;
        while (pointer <= right) {
            if (leftPtr == mid + 1) {
                temp[pointer++] = indexs[rightPtr++];
            }else if (rightPtr == right + 1) {
                counts[indexs[leftPtr]] += rightPtr - (mid + 1);
                temp[pointer++] = indexs[leftPtr++];
            }else if (nums[indexs[leftPtr]] <= nums[indexs[rightPtr]]) {
                counts[indexs[leftPtr]] += rightPtr - (mid + 1);
                temp[pointer++] = indexs[leftPtr++];
            }else {
                temp[pointer++] = indexs[rightPtr++];
            }
        }

        for (int i = left; i <= right; i++) {
            indexs[i] = temp[i];
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值