原题链接在这里:https://leetcode.com/problems/count-of-range-sum/

题目:

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 = -2upper = 2,
Return 3.
The three ranges are : [0, 0][2, 2][0, 2] and their respective sums are: -2, -1, 2.

题解:

题目的意思是说给了一个int array, 计算有多少subarray的sum在[lower, upper]区间内. 给的例子是index.

建立BST,每个TreeNode的val是prefix sum. 为了避免重复的TreeNode.val, 设置一个count记录多少个重复TreeNode.val, 维护leftSize, 记录比该节点value小的节点个数,rightSize同理.

由于RangeSum S(i,j)在[lower,upper]之间的条件是lower<=sums[j+1]-sums[i]<=upper. 所以我们每次insert一个新的PrefixSum sums[k]进这个BST之前,先寻找一下rangeSize该BST内已经有多少个PrefixSum, 叫它sums[t]吧, 满足lower<=sums[k]-sums[t]<=upper, 即寻找有多少个sums[t]满足:

sums[k]-upper<=sums[t]<=sums[k]-lower

BST提供了countSmaller和countLarger的功能,计算比sums[k]-upper小的RangeSum数目和比sums[k]-lower大的数目,再从总数里面减去,就是所求

Time Complexity: O(nlogn). Space: O(n).

AC Java:

 public class Solution {
public int countRangeSum(int[] nums, int lower, int upper) {
if(nums == null || nums.length == 0){
return 0;
}
int res = 0;
long [] sum = new long[nums.length+1];
for(int i = 1; i<sum.length; i++){
sum[i] = sum[i-1] + nums[i-1];
} TreeNode root = new TreeNode(sum[0]);
for(int i = 1; i<sum.length; i++){
res += rangeSize(root, sum[i]-upper, sum[i]-lower);
insert(root, sum[i]);
}
return res;
} private TreeNode insert(TreeNode root, long val){
if(root == null){
return new TreeNode(val);
}
if(root.val == val){
root.count++;
}else if(root.val > val){
root.leftSize++;
root.left = insert(root.left, val);
}else if(root.val < val){
root.rightSize++;
root.right = insert(root.right, val);
}
return root;
} private int countSmaller(TreeNode root, long val){
if(root == null){
return 0;
}
if(root.val == val){
return root.leftSize;
}else if(root.val > val){
return countSmaller(root.left, val);
}else{
return root.leftSize + root.count + countSmaller(root.right, val);
}
} private int countLarget(TreeNode root, long val){
if(root == null){
return 0;
}
if(root.val == val){
return root.rightSize;
}else if(root.val > val){
return countLarget(root.left, val) + root.count + root.rightSize;
}else{
return countLarget(root.right, val);
}
} private int rangeSize(TreeNode root, long lower, long upper){
int total = root.leftSize + root.count + root.rightSize;
int smaller = countSmaller(root, lower);
int larger = countLarget(root, upper);
return total - smaller - larger;
}
} class TreeNode{
long val;
int count;
int leftSize;
int rightSize;
TreeNode left;
TreeNode right;
public TreeNode(long val){
this.val = val;
this.count = 1;
this.leftSize = 0;
this.rightSize = 0;
}
}

Reference: http://www.cnblogs.com/EdwardLiu/p/5138198.html

LeetCode Count of Range Sum的更多相关文章

  1. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  2. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  3. 327. Count of Range Sum

    /* * 327. Count of Range Sum * 2016-7-8 by Mingyang */ public int countRangeSum(int[] nums, int lowe ...

  4. leetcode@ [327] Count of Range Sum (Binary Search)

    https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...

  5. [LeetCode] 327. Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  6. 【LeetCode】327. Count of Range Sum

    题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...

  7. [Swift]LeetCode327. 区间和的个数 | Count of Range Sum

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  8. 327. Count of Range Sum(inplace_marge)

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  9. 327 Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

随机推荐

  1. ccc animation

    cc.Class({ extends: cc.Component, properties: { sheepAnim: { default: null, type: cc.Animation } }, ...

  2. 推荐两款PC健康小软件

    一.前言 对于经常需要坐在电脑前工作一整天的人来说,健康问题是不得不关注的.下面推荐我一直在用的两款体积非常小(几百KB)的健康小软件,也许可以在无形中保护你.提醒你. 1. FadeTop 这是一款 ...

  3. ACM 荷兰国旗问题

    荷兰国旗问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:1   描述 荷兰国旗有三横条块构成,自上到下的三条块颜色依次为红.白.蓝.现有若干由红.白.蓝三种颜色的条块序列,要 ...

  4. 洛谷 P1803 凌乱的yyy Label:Water 贪心

    题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) 所以,他想知道他最多能参加 ...

  5. C#中的IComparable 和 IComparer 接口,实现列表中的对象比较和排序

    借豆瓣某博主的话先对这两个接口进行一个解释: IComparable在要比较的对象的类中实现,可以比较该对象和另一个对象 IComparer在一个单独的类中实现,可以比较任意两个对象. 如果已经支持 ...

  6. git 放弃本地某个文件的修改,或所有修改

    18:57 2015/11/17git 放弃本地某个文件的修改,或所有修改git checkout 文件名git checkout // 放弃所有文件的所有修改git reset --hard 版本号 ...

  7. POJ 1654 Area(水题)

    题目链接 卡了一下精度和内存. #include <cstdio> #include <cstring> #include <string> #include &l ...

  8. URAL 1501. Sense of Beauty(记忆化搜索)

    题目链接 本来暴力写个TLE了,加上记忆化就A了. #include <cstring> #include <cstdio> #include <string> # ...

  9. 【BZOJ2002】 [Hnoi2010]Bounce 弹飞绵羊 分块/LCT

    Description 某天,Lostmonkey发明了一种超级弹力装置,为了在 他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonkey在地上沿着一条直线摆上n个装置,每个装 ...

  10. 集成IOS 环信SDK

    集成IOS SDK 在您阅读此文档时,我们假定您已经具备了基础的 iOS 应用开发经验,并能够理解相关基础概念. 下载SDK 通过Cocoapods下载地址 不包含实时语音版本SDK(EaseMobC ...