LeetCode 327. Count of Range Sum
无意看到的LeetCode新题,不算太简单,大意是给一个数组,询问多少区间和在某个[L,R]之内。首先做出前缀和,将问题转为数组中多少A[j]-A[i] (j>i)在范围内。
有一种基于归并排序的做法,在每次归并完左右两个子区间后,当前区间两部分分别都已经排序完毕,基于有序这一点,扫描后半段区间,对于每个A[i] (i>=mid),目标区间即为[ A[i]-R, A[i]-L ], 对于有序数组来说,求出元素落在某一区间的个数直接就是upper_bound-lower_bound,事实上,这里我们只需要另外两个浮动于前半段区间的指针即可动态维护upper_bound和lower_bound,因为查询目标区间的两个端点是不断递增的。
递归边界条件(只有一个数)做一下特判。LeetCode题目本身也需要注意许多边边角角的trick,比如输入vector为空,元素加加减减溢出的情况,所以直接无脑long long就好。
题外话,不知为何本题设计上输出是一个int,按理如果卡O(n^2)的话,数据规模必然做到可以构造答案溢出int_max的。
class Solution {
public:
int lo,hi;
int ret=;
void msort(vector<long long>& A,int x,int y,vector<long long>& T) {
if (y-x<=) {
if (A[x]>=lo&&A[x]<=hi) ret++;
return;
}
int mid=x+(y-x)/; msort(A,x,mid,T);
msort(A,mid,y,T);
int p=x,q=mid,it=x;
int j1=x,j2=x;
for (int i=mid;i<y;i++) {
while (j1<mid&&A[i]-A[j1]>=lo)
j1++;
while (j2<mid&&A[i]-A[j2]>hi)
j2++;
ret+=j1-j2;
}
while (p<mid||q<y) {
if (q>=y||(p<mid&&A[p]<=A[q]))
T[it++]=A[p++];
else
T[it++]=A[q++];
}
for (int i=x; i<y; i++)
A[i]=T[i];
}
int countRangeSum(vector<int>& nums, int lower, int upper) {
if (nums.size()==) return ;
ret=;
lo=lower,hi=upper;
vector<long long> temp(nums.size(),);
vector<long long> vec;
vec.push_back(nums[]);
for (int i=;i<nums.size();i++)
vec.push_back(nums[i]+vec[i-]);
msort(vec,,vec.size(),temp);
return ret;
}
};
LeetCode 327. Count of Range Sum的更多相关文章
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- [LeetCode] 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 (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
- 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 ...
- 327 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 区间和计数
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 ...
随机推荐
- 1441: Min
1441: Min Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 320 Solved: 213[Submit][Status][Discuss] De ...
- 1218: [HNOI2003]激光炸弹
1218: [HNOI2003]激光炸弹 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1139 Solved: 542[Submit][Statu ...
- javascript-基本数据类型和转换
ECMAScript中有5种基本数据类型:Undefined.Null.Boolean.Number.String.还有1种复杂数据类型-Object,Object实质上是由一组无序的名值对(键值对) ...
- 查看 NDK 版本
打开Android Studio , 打开左上角的菜单, File => Settings... 打开一个弹窗. 然后在 Appearance & Behavior =>Syst ...
- 【2017-03-24】CSS样式表
CSS样式表:层叠式样式表 一.样式表的分类 1.内联式 写在标记的属性位置,优先级最高,重用性最差. 格式: <div style="width:100px;height:100px ...
- Linux中的sed命令
sed - stream editor for filtering and transforming text 流编辑器的过滤和转换文本 sed [-nerf] [动作] 参数: -i 修改源文件 危 ...
- python编码问题之\"encode\"&\"decode\"
python encode decode 编码 decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换 ...
- MVC学习笔记3 - JsRender
许多发展平台减少代码和简化维护,使用模板和 HTML5 和 JavaScript 也不例外. JsRender 是一个 JavaScript 库使您可以一次定义一个样板文件结构,并使用它来动态地生成 ...
- 小程序新能力-个人开发者尝鲜微信小程序
个人开发者的福利 微信小程序,刚听到这个新名词的时候,我就兴冲冲的去找入口,看看自己能不能搞个微信小程序的HelloWorld,毕竟能在微信上把自己写的一些小工具跑起来还是满炫酷的. 没想,网上一查, ...
- 多线程图像处理中对选入DC的位图保护
我在应用多线程加速图像处理(具体参见图像处理的多线程计算)的过程中,曾遇到过一个线程同步的问题.多线程对图像不同子块进行处理,再合成.结果发现最终不是全部子块都处理成功,有的子块好像没有被处理.而且发 ...