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/...
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。