-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount of Smaller Numbers After Self.java
65 lines (58 loc) · 1.73 KB
/
Count of Smaller Numbers After Self.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
class TreeNode {
// left-children count(smaller), dpc(same as val)
// sum up the node turning right when insert(those smaller than)
int val, lc, dpc;
TreeNode left = null, right = null;
public TreeNode(int v){
val = v;
lc = 0;
dpc = 1;
}
}
List<Integer> re;
public List<Integer> countSmaller(int[] nums) {
// Augmented BST
re = new ArrayList<>();
if(nums.length == 0) return re;
// build backwards
TreeNode root = new TreeNode(nums[nums.length-1]);
re.add(0);
for(int i = nums.length-2; i >= 0; i--){
buildTree(root, nums[i], 0);
}
return re;
}
public void buildTree(TreeNode node, int val, int cnt){
// same: cnt + plus leftchildren
if(node.val == val){
cnt += node.lc;
node.dpc++;
re.add(0, cnt);
}
// right: update cnt with lc and dpc
else if(node.val < val){
cnt += node.lc + node.dpc;
if(node.right != null){
buildTree(node.right, val, cnt);
}
else{
re.add(0, cnt);
TreeNode n = new TreeNode(val);
node.right = n;
}
}
// left: update lc
else{
node.lc++;
if(node.left != null){
buildTree(node.left, val, cnt);
}
else{
re.add(0, cnt);
TreeNode n = new TreeNode(val);
node.left = n;
}
}
}
}