[LeetCode] 327. 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:
Input: nums =[-2,5,-1], lower =-2, upper =2,
Output: 3
Explanation: The three ranges are :[0,0],[2,2],[0,2]and their respective sums are:-2, -1, 2.
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
这道题给了我们一个数组,又给了一个下限和一个上限,让求有多少个不同的区间使得每个区间的和在给定的上下限之间。这道题的难度系数给的是 Hard,的确是一道难度不小的题,题目中也说了 Brute Force 的方法太 Naive 了,只能另想方法了。To be honest,这题超出了博主的能力范围,所以博主也没挣扎了,直接上网搜大神们的解法啦。首先根据前面的那几道类似题 Range Sum Query - Mutable,Range Sum Query 2D - Immutable 和 Range Sum Query - Immutable 的解法可知类似的区间和的问题一定是要计算累积和数组 sums 的,其中 sum[i] = nums[0] + nums[1] + ... + nums[i],对于某个i来说,只有那些满足 lower <= sum[i] - sum[j] <= upper 的j能形成一个区间 [j, i] 满足题意,目标就是来找到有多少个这样的 j (0 =< j < i) 满足 sum[i] - upper =< sum[j] <= sum[i] - lower,可以用 C++ 中由红黑树实现的 multiset 数据结构可以对其中数据排序,然后用 upperbound 和 lowerbound 来找临界值。lower_bound 是找数组中第一个不小于给定值的数(包括等于情况),而 upper_bound 是找数组中第一个大于给定值的数,那么两者相减,就是j的个数,参见代码如下:
解法一:
class Solution {
public:
int countRangeSum(vector<int>& nums, int lower, int upper) {
int res = ;
long long sum = ;
multiset<long long> sums;
sums.insert();
for (int i = ; i < nums.size(); ++i) {
sum += nums[i];
res += distance(sums.lower_bound(sum - upper), sums.upper_bound(sum - lower));
sums.insert(sum);
}
return res;
}
};
我们再来看一种方法,这种方法的思路和前一种一样,只是没有 STL 的 multiset 和 lower_bound 和 upper_bound 函数,而是使用了 Merge Sort 来解,在混合的过程中,已经给左半边 [start, mid) 和右半边 [mid, end) 排序了。当遍历左半边,对于每个i,需要在右半边找出k和j,使其满足:
j是第一个满足 sums[j] - sums[i] > upper 的下标
k是第一个满足 sums[k] - sums[i] >= lower 的下标
那么在 [lower, upper] 之间的区间的个数是 j - k,同时也需要另一个下标t,用来拷贝所有满足 sums[t] < sums[i] 到一个寄存器 Cache 中以完成混合排序的过程,这个步骤是混合排序的精髓所在,实际上这个寄存器的作用就是将 [start, end) 范围内的数字排好序先存到寄存器中,然后再覆盖原数组对应的位置即可,(注意这里 sums 可能会整型溢出,使用长整型 long 代替),参见代码如下:
解法二:
class Solution {
public:
int countRangeSum(vector<int>& nums, int lower, int upper) {
vector<long> sums(nums.size() + , );
for (int i = ; i < nums.size(); ++i) {
sums[i + ] = sums[i] + nums[i];
}
return countAndMergeSort(sums, , sums.size(), lower, upper);
}
int countAndMergeSort(vector<long>& sums, int start, int end, int lower, int upper) {
if (end - start <= ) return ;
int mid = start + (end - start) / ;
int cnt = countAndMergeSort(sums, start, mid, lower, upper) + countAndMergeSort(sums, mid, end, lower, upper);
int j = mid, k = mid, t = mid;
vector<int> cache(end - start, );
for (int i = start, r = ; i < mid; ++i, ++r) {
while (k < end && sums[k] - sums[i] < lower) ++k;
while (j < end && sums[j] - sums[i] <= upper) ++j;
while (t < end && sums[t] < sums[i]) cache[r++] = sums[t++];
cache[r] = sums[i];
cnt += j - k;
}
copy(cache.begin(), cache.begin() + t - start, sums.begin() + start);
return cnt;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/327
类似题目:
Range Sum Query 2D - Immutable
Count of Smaller Numbers After Self
参考资料:
https://leetcode.com/problems/count-of-range-sum/
https://leetcode.com/problems/count-of-range-sum/discuss/77990/Share-my-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 327. Count of Range Sum 区间和计数的更多相关文章
- 327 Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- 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
无意看到的LeetCode新题,不算太简单,大意是给一个数组,询问多少区间和在某个[L,R]之内.首先做出前缀和,将问题转为数组中多少A[j]-A[i] (j>i)在范围内. 有一种基于归并排序 ...
- 327. Count of Range Sum
/* * 327. Count of Range Sum * 2016-7-8 by Mingyang */ public int countRangeSum(int[] nums, int lowe ...
- 【LeetCode】327. Count of Range Sum
题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...
- 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 ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
随机推荐
- 从游击队到正规军:马蜂窝旅游网的IM系统架构演进之路
本文引用自马蜂窝公众号,由马蜂窝技术团队原创分享. 一.引言 今天,越来越多的用户被马蜂窝持续积累的笔记.攻略.嗡嗡等优质的分享内容所吸引,在这里激发了去旅行的热情,同时也拉动了马蜂窝交易的增长.在帮 ...
- python-10-列表、元组嵌套
前言 元组.列表前面章节有讲解实例,本节内容是列表.元组的多嵌套. 一.列表嵌套 1.列表嵌套操作1 # 列表的嵌套 li = ['xiaolong', '小林', ['小龙', 'xiaol'], ...
- UVA 291 The House Of Santa Claus DFS
题目: In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you re ...
- 移动端rem布局,用户调整手机字体大小或浏览器字体大小后导致页面布局出错问题
一.用户修改手机字体设置大小,影响App里打开的web页面. 手机字体设置大小,影响App的页面.Android的可以通过webview配置webview.getSettings().setTextZ ...
- WPF 3D相机基本坐标简介
基本概念 WPF中3D空间基本坐标系是右手坐标系. WPF中3D空间的原点是(0,0,0) Position: 这个参数用来表示相机在空间内的坐标.参数是(X,Y,Z).当修改相机的这个参数时,这个坐 ...
- CentOS 8 换源,设置dnf / yum镜像
aliyun更新了centos8的说明 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos- ...
- SqlServer 开篇简介
实例:我们的电脑中可以安装一个或多个SqlServer实例,每一个SqlServer实例可以包含一个或者多个数据库. 架构:数据库中,又有一个或者多个架构.架构里面包含:表,视图,存储过程. 文件与文 ...
- maven 学习---Maven自动化部署
在项目开发中,通常是部署过程包含以下步骤 检入代码在建项目全部进入SVN或源代码库中,并标记它. 从SVN下载完整的源代码. 构建应用程序. 生成输出要么WAR或EAR文件存储到一个共同的网络位置. ...
- jQuery的$().each()与$.each()的区别
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Python元组与字符串操作(9)——随机数、元组、命名元组
随机数 import random #导入random模块 randint(a,b) 返回[a,b]之间的整数 random.randint(0,9) randrange([start],stop,[ ...