【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:
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
.
提示:
这道题最直观的一个想法就是枚举出所有的子数组,然后检查他们是否在要求的取值范围内,这种方法的时间复杂度是O(n^2)的,显然会超时。
看到这种题目最容易想到的是什么呢?Two Pointers!对,但是在这道题上仅仅使用Two Pointers肯定是不够的,在Two Pointers的思想基础上,融合归并排序,就能找到一个比较好的解决方案。
这里我们的排序对象是前缀求和数组,在归并排序的合并阶段,我们有左数组和右数组,且左和右数组都是排好序的,所以我们可以用i遍历左数组,j,k两个指针分别取在右数组搜索,使得:
- sums[j] - sums[i] < upper
- sums[k] - sums[i] >= lower
那么此时,我们就找到了j-k个符合要求的子数组。
由于左右数组都是排好序的,所以当i递增之后,j和k的位置不用从头开始扫描。
最后还有一点需要注意的就是,为了防止溢出,我们的vector容纳的是long long型元素。
代码:
class Solution {
public:
int countRangeSum(vector<int>& nums, int lower, int upper) {
int n = nums.size();
if (n <= ) {
return ;
}
vector<long long> sums(n + , );
for (int i = ; i < n; ++i) {
sums[i+] = sums[i] + nums[i];
}
return merge(sums, , n, lower, upper);
} int merge(vector<long long>& sums, int start, int end, int lower, int upper) {
if (start >= end) {
return ;
}
int mid = start + (end - start) / ;
int count = merge(sums, start, mid, lower, upper) + merge(sums, mid + , end, lower, upper);
vector<long long> tmp(end - start + , );
int j = mid + , k = mid + , t = mid + , i = start, r = ;
for (; i <= mid; ++i, ++r) {
while (j <= end && sums[j] - sums[i] <= upper) ++j;
while (k <= end && sums[k] - sums[i] < lower) ++k;
count += j - k;
while (t <= end && sums[t] <= sums[i]) tmp[r++] = sums[t++];
tmp[r] = sums[i];
}
for (int i = ; i < r; ++i) {
sums[start + i] = tmp[i];
}
return count;
}
};
【LeetCode】327. Count of Range Sum的更多相关文章
- 327. Count of Range Sum
/* * 327. Count of Range Sum * 2016-7-8 by Mingyang */ public int countRangeSum(int[] nums, int lowe ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- 【leetcode】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- [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(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 327. Count of Range Sum
无意看到的LeetCode新题,不算太简单,大意是给一个数组,询问多少区间和在某个[L,R]之内.首先做出前缀和,将问题转为数组中多少A[j]-A[i] (j>i)在范围内. 有一种基于归并排序 ...
随机推荐
- 读Zepto源码之集合操作
接下来几个篇章,都会解读 zepto 中的跟 dom 相关的方法,也即源码 $.fn 对象中的方法. 读Zepto源码系列文章已经放到了github上,欢迎star: reading-zepto 源码 ...
- 《安卓网络编程》之第六篇 Android中的WIFI和蓝牙
关于WIFI就不多介绍啦,直接来个段子吧. 问:“WiFi对人体有伤害么?” 答:“不清楚,反正没有WiFi我就浑身不舒服. 比较重要的一点就是WifiManager wm=(WifiManager ...
- 1.WF 4.5在项目中直接使用的问题
最近公司需要在互联网产品后台进行精细化流程管理,开发了一个基于WF 4.5框架的流程引擎与图形化设计器,让流程真正的跑了起来. 基于Visual Studio 直接设计流程主要面临以下的问题: 1.需 ...
- JSP中include指令和include动作区别
首先 <%@ include file=” ”%>:为指令元素 <jsp:include page=” ” flush=”true”/>:为 动作元素 先说指令元素: incl ...
- .net实现多重继承问题(virtual)
C#中是没有类的多重继承这个概念.要使用多重继承必须要通过接口Interface来完成, 一.接口类 interface getTable{ DataTable Getdatatable( ...
- 【持续集成】GIT+jenkins+snoar——jenkins发布php、maven项目
一.持续集成 1.1 什么是持续集成? continuous integration (CI),持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员,每天至少集成一次,也就意味着 ...
- php原生curl接口的请求
/** * @desc 接口请求处理 * @date 2017/5/19 11:39 * @param [$url请求的接口地址,$way为false为get请求,true为post请求] * @au ...
- Javascript创建类和对象
现总结一下Javascript创建类和对象的几种方法: 1.原始的创建方法: <script type="text/javascript"> var person = ...
- 【2017-05-30】WebForm文件上传
用 FileUpload控件进行上传文件. <asp:FileUpload ID="FileUpload1" runat="server" /> ...
- iOS CAReplicatorLayer 实现脉冲动画效果
iOS CAReplicatorLayer 实现脉冲动画效果 效果图 脉冲数量.速度.半径.透明度.渐变颜色.方向等都可以设置.可以用于地图标注(Annotation).按钮长按动画效果(例如录音按钮 ...