这一题,我们使用了分治法。

首先看时间复杂度为o(n^2),比较naïve的方法:

使用一个数组sum[],长度为原数组长度+1,每一个元素sum[i]为原数组中index0~i-1之和,显然当sum[j] – sum[i]就是i~j-1之和,于是我们只需要两个for来遍历所有[i, j],并且比较是否在lower~upper之间即可。

但是题目要求时间复杂度由于o(n^2),考虑使用分治

1) 分:将sum[]分为左右两部分,分别计算满足条件的[i,j]

2) 合:除了i,j都在左或者都在右,还有一种情况,i在左,j在右 想要复杂度为o(nlogn),由主方法易知,合这一步必须为线性复杂度。这里有一个小trick,在合这一步中我们对这一次迭代的sum[start]-sum[end]进行排序,这样,返回之后,对于上一层函数来说,左右两部分都已经排好了序,我们只需要检测i = start~mid,mid<j,k<=end然后找到第一个sum[k]-sum[i] > lower, 和第一个sum[j] – sum[i] > upper,然后j-k就是满足条件的范围的个数。

代码如下:

 class Solution {
public int countRangeSum(int[] nums, int lower, int upper) {
long[] sum = new long[nums.length+1]; for(int i=0; i<nums.length; i++){
sum[i+1] = sum[i] + nums[i];
} return mergeSort(sum, 0, nums.length + 1, lower, upper);
} private int mergeSort(long[] sum, int start, int end, int lower, int upper){
if(end - start <= 1)
return 0;
int mid = start + (end - start) / 2;
//int mid = (start + end) / 2;
int count = mergeSort(sum, start, mid, lower, upper) + mergeSort(sum, mid, end, lower, upper); long[] tmp = new long[end - start];
int j = mid, k = mid, t = mid;
for(int i = start, r = 0; i < mid; i++, r++){
while(k < end && sum[k] - sum[i] < lower)
k++;
while(j < end && sum[j] - sum[i] <= upper)
j++;
while(t < end && sum[t] < sum[i])
tmp[r++] = sum[t++];
tmp[r] = sum[i]; count += j - k;
} System.arraycopy(tmp, 0, sum, start, t - start);
return count;
}
}

LC327 Count of Range Number的更多相关文章

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

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

  2. Lintcode: Count of Smaller Number

    Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...

  3. LeetCode "Count of Smaller Number After Self"

    Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...

  4. Lintcode249 Count of Smaller Number before itself solution 题解

    [题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...

  5. 327. Count of Range Sum

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

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

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

  7. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

  8. 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 ...

  9. 【LeetCode】327. Count of Range Sum

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

随机推荐

  1. Eclipse 中安装 CDT 插件编写 C/C++

    使用到的软件 1.Eclipse 开发工具 2.MinGW 编译器 一.Eclipse 中安装 CDT 插件 打开 Eclipse 插件市场 搜索 CDT,并找到如下的插件.插件的版本名字可能不太一样 ...

  2. 2019-8-31-dotnet-特性-DynamicallyInvokable-是用来做什么的

    title author date CreateTime categories dotnet 特性 DynamicallyInvokable 是用来做什么的 lindexi 2019-08-31 16 ...

  3. Derby的安装与使用

    Derby数据库是一个纯用Java实现的内存数据库,属于Apache的一个开源项目.由于是用Java实现的,所以可以在任何平台上运行:另外一个特点是体积小,免安装,只需要几个小jar包就可以运行了. ...

  4. HTML5 上传图片预览

    html5出现之前如果需要上传图片预览 一般都是先上传到服务器然后远程预览 html5出现之后   有个filereader 解决了这问题 //选中图片之后 $("#fileAddPic&q ...

  5. dataset datatable 转json

    class ToJosn { #region dataTable转换成Json格式 /// <summary> /// dataTable转换成Json格式 /// </summar ...

  6. session过期跳转到登陆页面并跳出iframe框架的两个方法

    最近在做拦截器,判断用户登录后操作超时,失去权限然后要重新登录,但是用的iframe,返回的登陆页总是在框架中显示,我百度了下,总是只有其中一个方法,现在分享下两种解决方法,希望对你们有帮助: 方法一 ...

  7. 自己编写jquery插件

    http://blog.csdn.net/chenxi1025/article/details/52222327 https://www.cnblogs.com/ajianbeyourself/p/5 ...

  8. iOS开发之IMP和SEL(方法和类的反射)

    1.SEL:类方法的指针,相当于一种编号,区别与IMP! IMP:函数指针,保存了方法的地址! SEL是通过表取对应关系的IMP,进行方法的调用! 2.获取SEL和IMP方法和调用: SEL meth ...

  9. vue alert插件(标题为图片)(自写)

    <template> <div class="modelSelf"> <div class="model" @click=&quo ...

  10. GitHub for Visual Studio使用讲解

    从VS2015起(应该是吧?),微软已经在VS中集成了GitHub,方便开发者对项目进行版本控制. 扩展包下载地址:https://aka.ms/ghfvs 其实VS2015的安装包中已经自带了这个扩 ...