-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountRangeSum.java
More file actions
28 lines (28 loc) · 878 Bytes
/
Copy pathCountRangeSum.java
File metadata and controls
28 lines (28 loc) · 878 Bytes
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
import java.util.*;
public class CountRangeSum {
public int countRangeSum(int[] nums, int lower, int upper) {
TreeMap<Integer,Integer> map = new TreeMap();
int sum = 0;
map.put(0,1);
int total = 0;
for(int num:nums){
sum += num;
// lower<= sum - x <= upper
// sum -upper <= x <= sum-lower
// l <= x <= r
int r = sum-lower;
int l = sum-upper;
SortedMap<Integer,Integer> sub = map.subMap(l,r+1); // since it's exclusive
for(Integer count:sub.values()){
total += count;
}
if(!map.containsKey(sum))
map.put(sum,0);
map.put(sum,map.get(sum)+1);
}
return total;
}
public static void main(String[] args) {
int[] data = new int[]{230,136,145};
}
}