327. Count of Range Sum
题目链接:https://leetcode.com/problems...
这题实际就是给定范围内的range sum,divide and conquer的方法。一路计算prefixSum[0:i],并把结果放进tree里面,然后计算到prefixSum[0:j+1]的时候,找tree里面有没有满足条件的prefixSum[0:i],这里的条件是lower <= sum[0:j+1] - sum[0:i] <= upper
,那么可知sum[0:j+1] - upper <= sum[0:i] <= sum[0:j+1] - lower
,那么这个就一个recursion就好了。注意一开始把0加进去,考虑结果是sum[0:j]的情况,还有要用long型,以免sum会overflow
public class Solution {
public int countRangeSum(int[] nums, int lower, int upper) {
int n = nums.length;
if(n == 0) return 0;
// binary search tree
Node root = new Node(0);
int res = 0;
long prefixSum = 0;
for(int i = 0; i < n; i++) {
prefixSum += nums[i];
res += findNumInBound(root, lower, upper, prefixSum);
insert(root, prefixSum);
}
return res;
}
private int findNumInBound(Node node, long low, long up, long sum) {
// base case
if(node == null) return 0;
// range: sum - up <= node.val <= sum - low
if(node.val < sum - up) return findNumInBound(node.right, low, up, sum);
else if(node.val > sum - low) return findNumInBound(node.left, low, up, sum);
else return 1 + findNumInBound(node.left, low, up, sum) + findNumInBound(node.right, low, up, sum);
}
private void insert(Node node, long value) {
while(node != null) {
if(node.val > value) {
if(node.left == null) {
node.left = new Node(value);
break;
}
node = node.left;
}
else {
if(node.right == null) {
node.right = new Node(value);
break;
}
node = node.right;
}
}
}
class Node {
long val;
Node left;
Node right;
Node(long val) { this.val = val; }
}
}
还是可以binary index tree来做,要统计sum[0:j+1] - upper <= sum[0:i] <= sum[0:j+1] - lower
范围内的个数,就是用sum。参考博客:
http://bookshadow.com/weblog/...
public class Solution {
public int countRangeSum(int[] nums, int lower, int upper) {
int n = nums.length;
if(n == 0) return 0;
// prefix array
long[] prefixSum = new long[n];
for(int i = 0; i < n; i++) {
prefixSum[i] = (i > 0 ? prefixSum[i-1] : 0) + nums[i];
}
long[] sorted = Arrays.copyOf(prefixSum, prefixSum.length);
Arrays.sort(sorted);
// binary index tree
map = new HashMap();
int idx = 1;
for(long sum : sorted) {
if(!map.containsKey(sum)) map.put(sum, idx++);
}
// build tree
BIT t = new BIT(idx);
int res = 0;
for(int i = 0; i < n; i++) {
int l = binarySearch(sorted, prefixSum[i] - upper - 1);
int r = binarySearch(sorted, prefixSum[i] - lower);
res += t.sum(r) - t.sum(l);
if(prefixSum[i] >= lower && prefixSum[i] <= upper) res += 1;
t.add(map.get(prefixSum[i]), 1);
}
return res;
}
Map<Long, Integer> map;
// find the last element <= val
private int binarySearch(long[] arr, long val) {
int l = 0, r = arr.length - 1;
while(l < r) {
int mid = l + (r - l) / 2 + 1;
if(arr[mid] <= val) l = mid;
else r = mid - 1;
}
if(arr[l] > val) return 0;
return map.get(arr[l]);
}
class BIT {
int n;
int[] tree;
BIT(int n) { this.n = n; tree = new int[n]; }
protected int sum(int i) {
int res = 0;
while(i > 0) {
res += tree[i];
i -= i & -i;
}
return res;
}
protected void add(int i, int val) {
while(i < n) {
tree[i] += val;
i += i & -i;
}
}
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。