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 ...
随机推荐
- Swift应用案例 1.无限轮播
从今天开始,我学习的重点开始转向Swift,并且会分享一些自己学习的心得体会,今天给大家带来的的是无限轮播.广告页的无限轮播是非常常见的一个功能,大多数APP都有,大多数程序员也都实现过,今天我们 ...
- Archlinux中卸载 Slim
Slim 是图形登录器.最近停止更新了,据说在systemd中有兼容性问题. 卸载Slim的原因是某计算机使用的是AMD显卡的Catalyst驱动,图形驱动一旦出问题,Slim就无法启动,给维护造成困 ...
- vue学习笔记(一)关于事件冒泡和键盘事件 以及与Angular的区别
一.事件冒泡 方法一.使用event.cancelBubble = true来组织冒泡 <div @click="show2()"> <input type=&q ...
- jmeter的http cookies管理器使用
关于Cookie不过多介绍,测试UI的小伙伴们应该对此有深深的爱和恨~ 本文介绍如何:1.获取Cookie.2.保存Cookie 3.引用Cookie 最终达到Cookie类似无法失效的目的~ Coo ...
- tesseract ocr文字识别
一.环境搭建 (基于VS2010) 1.下载安装 tesseract-ocr-setup-3.02.02.exe 安装包 ,安装时候最好是在FQ的情况下安装.(安装一点要勾选 Tesseract de ...
- C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(下)
译文,个人原创,转载请注明出处(C# 6 与 .NET Core 1.0 高级编程 - 41 ASP.NET MVC(下)),不对的地方欢迎指出与交流. 章节出自<Professional C# ...
- iOS开发之pch文件
项目的Supporting files文件夹下面有个“工程名-Prefix.pch”文件,也是一个头文件 pch头文件的内容能被项目中的其他所有源文件共享和访问 一般在pch文件中定义一些全局的宏 在 ...
- 一次安装rpcbind失败引发的思考
问题: yum install rpcbind -y 出现如下错误: Error -.el6.x86_64 error: %pre(rpcbind--.el6.x86_64) scriptlet fa ...
- javascript重修之书(一):如何判断变量的数据类型
javascript重修之书(一):如何判断变量的数据类型 一:检测值类型 基本类型:(Undefined.Null.Boolean.Number和String) javascript之所以被称为一门 ...
- 为什么使用 Containjs 模块化管理工具效率高?
为什么使用 Containjs 模块化管理工具效率高? 要说明这个首先得说明一下,Containjs 的模块加载原理. 第一步,首先使用异步加载(ajax)在 js 目录下的 app.js 入口模块( ...