forked from Sunchit/Coding-Decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountOfSmallerNumbersAfterSelf.java
102 lines (82 loc) · 2.16 KB
/
CountOfSmallerNumbersAfterSelf.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
class Solution {
public class SegmentTreeNode {
public SegmentTreeNode left;
public SegmentTreeNode right;
public int Start;
public int End;
public int Sum;
public SegmentTreeNode(int start, int end) {
Start = start;
End = end;
Sum = 0;
}
}
public SegmentTreeNode buildTree(int start, int end) {
if (start > end) {
return null;
}
SegmentTreeNode node = new SegmentTreeNode(start, end);
if (start == end) {
return node;
}
int mid = start + (end - start) / 2;
node.left = buildTree(start, mid);
node.right = buildTree(mid + 1, end);
return node;
}
public void update(SegmentTreeNode node, int index) {
if (node == null) {
return;
}
if (node.Start == index && node.End == index) {
node.Sum += 1;
return;
}
int mid = node.Start + (node.End - node.Start) / 2;
if (index <= mid) {
update(node.left, index);
}
else {
update(node.right, index);
}
node.Sum = node.left.Sum + node.right.Sum;
}
public int SumRange(SegmentTreeNode root, int start, int end) {
if (root == null || start >end) {
return 0;
}
if (root.Start == start && root.End == end) {
return root.Sum;
}
int mid = root.Start + (root.End - root.Start) / 2;
if (end <= mid) {
return SumRange(root.left, start, end);
}
else if (start > mid) {
return SumRange(root.right, start, end);
}
return SumRange(root.left, start, mid) + SumRange(root.right, mid + 1, end);
}
public List<Integer> countSmaller(int[] nums) {
if (nums == null || nums.length == 0) {
return new ArrayList<>();
}
int[] counts = new int[nums.length];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int el : nums) {
min = Math.min(min, el);
max = Math.max(max, el);
}
SegmentTreeNode root = buildTree(min, max);
for (int i = nums.length - 1; i >= 0; i--) {
update(root, nums[i]);
counts[i] = SumRange(root, min, nums[i] - 1);
}
List<Integer> ans = new ArrayList<>();
for (int i : counts) {
ans.add(i);
}
return ans;
}
}