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

首先看时间复杂度为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. Tomcat的优化技巧

    (1)参数 minProcessors:最小空闲连接线程数,用于提高系统处理性能,默认值为10. maxProcessors:最大连接线程数,即:并发处理的最大请求数,默认值为75. acceptCo ...

  2. 2019-6-23-天河2-程序-version-GLIBCXX_3.4.21-not-found-解决方法

    title author date CreateTime categories 天河2 程序 version GLIBCXX_3.4.21 not found 解决方法 lindexi 2019-06 ...

  3. Easy Excel导出

    @GetMapping(value = "/down2") public void down2(HttpServletResponse response) throws Excep ...

  4. odoo 下 get_object_reference 函数

    get_object_reference是 ir.model.data 模块中下的一个函数 该函数通过调用ir.model.data 模块中另外一个函数 xmlid_lookup 返回结果 def g ...

  5. Intel RealSense Depth Camera D435安装ROS 驱动——Ubuntu16.04

    官方教程 软件包下载链接 https://github.com/IntelRealSense/realsense-ros Download/Clone librealsense github repo ...

  6. Mysql修改表备注, 列信息

    1.添加表和字段的注释 创建数据表的同时,给表和字段添加注释 -- 创建用户信息表 CREATE TABLE tb_user ( id INT AUTO_INCREMENT PRIMARY KEY C ...

  7. Docker在线文档收集

    极客学院 kubernetes中文社区 易百教程

  8. php面向对象成员方法(函数)练习

    <?php header('content-type:text/html;charset=utf-8'); //成员方法的举例 /* ①添加sayHello 成员方法,输出 'hello' ②添 ...

  9. 使用xshell远程连接到linux

      1.检查是否安装ssh rpm -qa | grep ssh 已安装是这样 如果没有安装,则 yum install openssh* #命令安装 2.开启ssh服务 [root@localhos ...

  10. An invalid property 'jdbcType ' was found in mapping

    大概2种原因: 1 放进去的类型与字段的类型不匹配 2 比较变态,xml中=两边不能有空格! 错误示例如下:  #{plat,jdbcType = INTEGER}, 去掉空格后: #{plat,jd ...