LC327 Count of Range Number
这一题,我们使用了分治法。
首先看时间复杂度为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的更多相关文章
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- 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 ...
- LeetCode "Count of Smaller Number After Self"
Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...
- 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 ...
- 327. Count of Range Sum
/* * 327. Count of Range Sum * 2016-7-8 by Mingyang */ public int countRangeSum(int[] nums, int lowe ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
- 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 ...
- 【LeetCode】327. Count of Range Sum
题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...
随机推荐
- MATLAB 中自定义函数的使用
MATLAB在文件内部(在函数内部)定义函数,但文件名以开头函数来命名,与Java中每个文件只能有一个公开类,但在文件内部还是可以定义其他非公开类一个道理. 无参函数 do.m function do ...
- UMP系统功能 数据安全
- Ubuntu GitHub操作——创建仓库
一.创建仓库 1.初始化仓库(只在初始化时执行一次) git init 这部是新建并初始化仓库 2.向仓库中添加文件 git add . 将文件夹内的所有文件都添加到仓库中(注意小“.”),在这部之后 ...
- centos6 php7 安装 memcache 和 memcached
下载安装memcache 注意:官网的memcache包,暂时好像不支持php7.所以到下面地址下载memcache包,切换到php7分支 php7 memcache github 下载地址 官网下载 ...
- VUE环境下获取当前时间并格式化--按秒数更新
<el-col :span="8"><div class="grid-content title-time"> {{date}}< ...
- VS2012 TFS 解决计算机改名无法连接TFS的问题
闲着没事改了下计算机名字,结果造成TFS无法连接. 报错讯息如下: ---------------------------Microsoft Visual Studio-------------- ...
- 转:Wireshark基本介绍和学习TCP三次握手
源地址:http://www.cnblogs.com/TankXiao/archive/2012/10/10/2711777.html 之前写过一篇博客:用 Fiddler 来调试HTTP,HTTPS ...
- PAT甲级——A1095 Cars on Campus
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- https://blog.csdn.net/qq_33169863/article/details/82977791
https://blog.csdn.net/qq_33169863/article/details/82977791 ** 查看设备连接 adb devices ** 列出手机已安装的包名 adb ...
- js (function(){}()),(function(){})(),$(function(){});之间的区别
参考:https://blog.csdn.net/stpice/article/details/80586444 (function(){}()), (function(){})() 均为立即执行函数 ...