題目連接:https://leetcode.com/problems...java
divide and conquer的題,用bst來作,這種求有多少smaller的題通常都是bst。node裏多加一個信息:size表示以node爲subtree的節點數。node
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來作映射。ide
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/...this