[LeetCode] Count of Range Sum
来源:互联网 发布:淘宝店铺类目修改2016 编辑:程序博客网 时间:2024/06/05 20:29
题目:
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 = -2, upper = 2,
Return 3.
The three ranges are : [0, 0], [2, 2], [0, 2] and their respective sums are: -2, -1, 2.
题目大意:给一个上下界,找出在上下界之内的范围的总数。
比如题目中-2,5,-1。 -2在范围中,-2+5 = 3不在范围中,-2+5+(-1) = 2在范围中,5+(-1)不在范围中,-1在范围中,故而总数是3。
上面分析题目的过程,其实就是一个遍历所有范围和的暴力解法。复杂度为O(n2)。
这道题同样可以采用分治的思想,利用归并排序实现:
这道题目,首先有一个预处理的过程,也是比较难想到的过程,就是预先就出每一段的累加和。
比如示例中,-2,5,-1。那么利用一个数组sums,sums[0]就是前0个元素和也就是0,sum[1]就是前1个元素和-2,sum[2]就是-2+5=3,以此类推。
然后呢,对sums进行归并排序,对,就是对累加和进行归并排序!
之所以利用归并排序,就是因为归并排序的稳定性,此外就是归并的过程中,左右两部分是局部有序的,这两个性质。
稳定性
首先来看第一点,在每一次归并排序的过程中,对于两边的元素,都是稳定的。比如归并两个数组1,2,3和4,5,6的时候,左边的元素出现在原数组中的位置一定在右边的元素的左边!比较拗口,举个例子,归并排序4,3,2,1,利用自顶向上递归归并,首先会排出 3,4,1,2,然后对 3,4 和1,2归并,3在原数组中位置一定在1和2的左边,4也是同理,这种分边稳定性,对于此题目,是很重要的一点!
这点的意义就是,对于我们的sums归并时,左边取第i个元素,右边取第j个元素,那么sums[j] - sums[i]就是从j到i的范围和!
局部有序性
第一轮:
为了找lower到upper之间的范围和,我们每次在归并的过程中,左边取一个指针i,右边取j,k。
那么sums[j]-sums[i] 是 范围和S(i,j), 同理有S(i,k),而且S(i,j)到S(i,k)是递增的,因为两部分都是有序的!
这样我们可以移动j 直到S(i,j)>=lower,在移动k直到S(i,k) > upper!这样,k-j就是找到的范围数目!
第二轮:
然后,再移动i,注意i增加了表明sums[i]增大,那么sums[j]-sums[i]减小,所以不需要向左移动j,因为那样只会使S(i,j)更小!而之前的S(i,j)是>=lower的第一个数,不存在更小的j使S(i,j)>=lower了!所以 j指针继续往右走即可,同理k也是。我们继续上一轮的判断
可以看到,操作的方式类似双指针,利用到的是归并排序的稳定性和局部有序性!
代码如下:
public class Solution { public int countRangeSum(int[] nums, int lower, int upper) { int n = nums.length; long[] sums = new long[n + 1]; for (int i = 0; i < n; ++i) sums[i + 1] = sums[i] + nums[i]; int r = mergeCount(sums, 0, n, lower, upper); return r; } private int mergeCount(long[] sums, int start, int end, int lower, int upper) { if (start >= end) return 0; int mid = (start + end) / 2; int count = mergeCount(sums, start, mid, lower, upper) + mergeCount(sums, mid + 1, end, lower, upper); long[] aux = new long[end - start + 1]; int j = mid + 1,k = mid + 1,r = mid + 1; for (int i = start,t = 0;i<=mid;i++) { //确定左右边界 while (j<=end && (sums[j] - sums[i])<lower) j++; while (k<=end &&(sums[k] - sums[i]<=upper)) k++; //归并排序,取出右边比左边小的数组放到aux中。 while (r<= end && sums[r] < sums[i]) aux[t++] = sums[r++]; aux[t++] = sums[i]; count += k - j; } //注意,这里右边数组并没有不一定全部放在aux中,因为左边可能先用完 //注意归并排序稳定的特性。 System.arraycopy(aux,0,sums,start,r-start); return count; }}
- leetcode Count of Range Sum
- LeetCode Count of Range Sum
- [LeetCode]Count of Range Sum
- Leetcode Count of Range Sum
- leetcode Count of Range Sum
- []LeetCode]Count of Range Sum
- [LeetCode] Count of Range Sum
- Count of Range Sum(leetcode)
- <LeetCode OJ> 327. Count of Range Sum
- leetcode 327. Count of Range Sum
- Leetcode 327. Count of Range Sum[hard]
- LeetCode 327. Count of Range Sum
- leetcode 327.Count of Range Sum
- Leetcode #327 Count of Range Sum
- [leetcode]Count of Range Sum
- LeetCode 327. Count of Range Sum
- LeetCode 327 Count of Range Sum
- LeetCode 327. Count of Range Sum
- 学会封装自己的class类:
- angular自定义指令templateUrl
- c# 数字转大写中文
- 功角自平衡 摘自张幸浩的《永磁同步电机的低成本控制系统研究》
- Android开发——四大组件BroadcastReceiver实例广播事件的拦截转发
- [LeetCode] Count of Range Sum
- BeanFactory作为Spring容器,程序需要手动获取Bean后处理器,然后手动注册!
- oracle学习之:三种方式查看SQL语句的执行计划
- sql语句中GROUP BY 和 HAVING的使用 count()
- git的使用心得,希望对大家都有用,有什么疑问尽管留言
- ISD9160学习笔记06_玩票项目OpenHamtaro
- c#线程之各种类读书笔记
- 初来乍到
- js前台分页显示后端JAVA数据响应