-
Notifications
You must be signed in to change notification settings - Fork 130
/
Count of Range Sum.js
103 lines (83 loc) · 2.45 KB
/
Count of Range Sum.js
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
103
/**
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.
Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive.
Note:
A naive algorithm of O(n2) is trivial. You MUST do better than that.
Example:
Given nums = [-2, 5, -1], lower = -2, upper = 2,
Return 3.
The three ranges are : [0, 0], [2, 2], [0, 2] and their respective sums are: -2, -1, 2.
*/
/**
* @param {number[]} nums
* @param {number} lower
* @param {number} upper
* @return {number}
*/
var countRangeSum = function(nums, lower, upper) {
function SegmentTreeNode(min, max) {
this.min = min;
this.max = max;
this.count = 0;
}
function buildSegmentTree(valArr, low, high) {
if (low > high) {
return null;
}
var mid = parseInt((low + high) / 2),
node = new SegmentTreeNode(valArr[low], valArr[high]);
if (low === high) {
return node;
}
node.left = buildSegmentTree(valArr, low, mid);
node.right = buildSegmentTree(valArr, mid + 1, high);
return node;
}
function updateSegmentTree(node, val) {
if (!node) {
return;
}
if (val >= node.min && val <= node.max) {
node.count++;
updateSegmentTree(node.left, val);
updateSegmentTree(node.right, val);
}
}
function rangeCount(node, min, max) {
if (!node) {
return 0;
}
if (min > node.max || max < node.min) {
return 0;
}
if (min <= node.min && max >= node.max) {
return node.count;
}
return rangeCount(node.left, min, max) + rangeCount(node.right, min, max);
}
var len = nums.length,
sumSet = new Set(),
sum = 0,
result = 0,
arr,
root,
i;
if (len === 0) {
return 0;
}
for (i = 0; i < len; i++) {
sum += nums[i];
sumSet.add(sum);
}
arr = Array.from(sumSet);
arr.sort(function(a, b) {
return a - b;
});
root = buildSegmentTree(arr, 0, arr.length - 1);
for (i = len - 1; i >= 0; i--) {
updateSegmentTree(root, sum);
sum -= nums[i];
result += rangeCount(root, lower + sum, upper + sum);
}
return result;
};