315. Count of Smaller Numbers After Self

315. Count of Smaller Numbers After Self

题目链接:https://leetcode.com/problems...

divide and conquer的题,用bst来做,这种求有多少smaller的题一般都是bst。node里多加一个信息:size表示以node为subtree的节点数。

public class Solution {
    public List<Integer> countSmaller(int[] nums) {
        /* binary search tree 
         */
        int n = nums.length;
        LinkedList<Integer> res = new LinkedList();
        if(n == 0) return res;
        
        res.add(0);
        Node root = new Node(nums[n-1]);
        for(int i = n - 2; i >= 0; i--) {
            res.addFirst(findSmaller(root, nums[i]));
        }
        return res;
    }
    
    private int findSmaller(Node root, int value) {
        int res = 0;
        while(root != null) {
            root.size += 1;
            if(root.val < value) {
                // add root and all left nodes
                res += 1 + (root.left == null ? 0 : root.left.size);
                if(root.right == null) {
                    root.right = new Node(value);
                    break;
                }
                root = root.right;
            }
            else {
                if(root.left == null) {
                    root.left = new Node(value);
                    break;
                }
                root = root.left;
            }
        }
        return res;
    }
    
    class Node {
        int val;
        Node left;
        Node right;
        // count the size of this subtree
        int size;
        Node(int val) { this.val = val; this.size = 1; }
    }
}

binary index tree也可以做,因为是统计有多少smaller的,其实就是求从最小值到nums[i] - 1的sum。tree的index是nums[i],要做一个映射,把nums[i]的值映射到[1, # of unique numbers in nums]之间。所以先把array给sort一下,用一个map来做映射。

public class Solution {
    public List<Integer> countSmaller(int[] nums) {
        /* binary index tree 
         */
        // reflection first, key: nums[i], value: order
        Map<Integer, Integer> map = new HashMap();
        int[] sorted = Arrays.copyOf(nums, nums.length);
        Arrays.sort(sorted);
        // record the order
        int idx = 1;
        for(int i = 0; i < nums.length; i++) {
            if(!map.containsKey(sorted[i])) map.put(sorted[i], idx++);
        }
        // range will be [1, idx]
        BIT t = new BIT(idx);
        LinkedList<Integer> res = new LinkedList();
        for(int i = nums.length - 1; i >= 0; i--) {
            int sum = t.sum(map.get(nums[i]) - 1);
            res.addFirst(t.sum(map.get(nums[i]) - 1));
            t.add(map.get(nums[i]), 1);
        }
        return res;
    }
    
    class BIT {
        int[] tree;
        int n;
        BIT(int n) { this.n = n; tree = new int[n]; }
        // sum the smaller elements
        protected int sum(int i) {
            int res = 0;
            while(i > 0) {
                res += tree[i];
                i -= (i & -i);
            }
            return res;
        }
        
        protected void add(int i, int val) {
            while(i < n) {
                tree[i] += val;
                i += (i & -i);
            }
        }
    }
}

还有merge sort的方法,参考discussion:
https://discuss.leetcode.com/...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值