Skip to content

Commit bc05847

Browse files
authored
Update and rename 327.Count of Range Sum.cpp to 327.Count-of-Range-Sum.cpp
1 parent 54db1bb commit bc05847

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Solution {
2+
int result;
3+
long temp[10001];
4+
public:
5+
int countRangeSum(vector<int>& nums, int lower, int upper)
6+
{
7+
int n = nums.size();
8+
vector<long>presum(n+1);
9+
for (int i=0; i<n; i++)
10+
presum[i+1] = presum[i]+nums[i];
11+
DivideConque(presum,0,n,lower,upper);
12+
return result;
13+
}
14+
15+
void DivideConque(vector<long>& nums, int a, int b, int lower, int upper)
16+
{
17+
if (a>=b) return;
18+
int mid = a+(b-a)/2;
19+
DivideConque(nums,a,mid,lower,upper);
20+
DivideConque(nums,mid+1,b,lower,upper);
21+
22+
for (int i=a; i<=mid; i++)
23+
{
24+
auto p1 = lower_bound(nums.begin()+mid+1,nums.begin()+b+1,nums[i]+lower);
25+
auto p2 = upper_bound(nums.begin()+mid+1,nums.begin()+b+1,nums[i]+upper);
26+
result+=p2-p1;
27+
}
28+
29+
int i=a, j=mid+1, p = 0;
30+
while (i<=mid && j<=b)
31+
{
32+
if (nums[i]<=nums[j])
33+
{
34+
temp[p] = nums[i];
35+
i++;
36+
}
37+
else
38+
{
39+
temp[p] = nums[j];
40+
j++;
41+
}
42+
p++;
43+
}
44+
while (i<=mid)
45+
{
46+
temp[p] = nums[i];
47+
i++;
48+
p++;
49+
}
50+
while (j<=b)
51+
{
52+
temp[p] = nums[j];
53+
j++;
54+
p++;
55+
}
56+
for (int i=0; i<b-a+1; i++)
57+
nums[a+i] = temp[i];
58+
}
59+
};

Divide_Conquer/327.Count-of-Range-Sum/327.Count of Range Sum.cpp

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)