-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount of Range Sum.cs
More file actions
54 lines (43 loc) · 1.46 KB
/
Count of Range Sum.cs
File metadata and controls
54 lines (43 loc) · 1.46 KB
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
public class Solution {
public int CountRangeSum(int[] nums, int lower, int upper) {
if (nums == null || nums.Length == 0) {
return 0;
}
var n = nums.Length;
var sums = new long[n + 1];
for (var i = 0; i < n; i++) {
sums[i + 1] = sums[i] + nums[i];
}
return MergeSort(sums, 0, n, lower, upper);
}
private int MergeSort(long[] sums, int low, int high, int lower, int upper) {
if (low == high) {
return 0;
}
var mid = low + (high - low) / 2;
var count = MergeSort(sums, low, mid, lower, upper) + MergeSort(sums, mid + 1, high, lower, upper);
var temp = new long[high - low + 1];
var j = mid + 1;
var k = mid + 1;
var r = mid + 1;
var t = 0;
for (var i = low; i <= mid; i++) {
while (k <= high && sums[k] - sums[i] < lower) {
k++;
}
while (j <= high && sums[j] - sums[i] <= upper) {
j++;
}
while (r <= high && sums[r] <= sums[i]) {
temp[t++] = sums[r++];
}
temp[t++] = sums[i];
count += j - k;
}
while (r <= high) {
temp[t++] = sums[r++];
}
Array.Copy(temp, 0, sums, low, temp.Length);
return count;
}
}