【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)在范围内. 有一种基于归并排序 ...
随机推荐
- [笔记]ACM笔记 - 自用模板
长期更新. 快速幂 lld pow_mod(lld a, lld b, const int &pr) { lld ans = 1; while (b) { if (b & 1) ans ...
- Jquery ajaxSubmit()的浏览器兼容问题
form.ajaxSubmit({ 2 beforeSubmit: function() { 3 if (FinanceUtil.validate(form)) { 4 FinanceUtil.loa ...
- 【设计模式】之开闭原则(OCP)
开闭原则是面向对象设计的一个重要原则,其定义如下: 开闭原则(Open-Closed Principle, OCP):一个软件实体应当对扩展开放,对修改关闭.即软件实体应尽量在不修改原有代码的情况下进 ...
- 把ipad作为电脑的第二显示器
需要:1,iPad 2,ios端需要软件idisplay(在pp助手里搜索idisplay下载即可) 3,windows需要软件iDisplay(链接http://www ...
- java虚拟机学习-JVM调优总结-典型配置举例(10)
以下配置主要针对分代垃圾回收算法而言. 堆大小设置 年轻代的设置很关键 JVM中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制:系统的可用虚拟内存限制:系统的可用物理 ...
- VR大时代-全景智慧城市搭建是一个任重而道远的任务
全景智慧城市搭建是一个任重而道远的任务,但是也促进了实体市场的蓬勃发展与进步.VR技术改变了人们以往的娱乐方式,而全景智慧城市将会彻底改变人们的生活习惯.VR是未来的计算平台,更是人力发展历史中,技术 ...
- VR全景智慧城市,完美的将虚拟与现实结合
很多人都粗浅的认为,VR虚拟智慧城市只是简单的将智慧城市和虚拟现实相结合的产物,这样的VR虚拟智慧城市看起来更像是个VR内容产品,而非城市建设成果.但是我们换个角度来思考的话,现在很多VR虚拟智慧城市 ...
- VR全景智慧城市,开启“上帝视角”体验‘身临其境’
VR全景把不同的场景 分为若干个VR视角点 进入一个视角点,用户便能开启"上帝视角" 转动手机,身临其境地360度转动察看,对场景的全貌和细节一目了然. 人生处处有尴尬 比如大 ...
- php curl 的几个实例
使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并把 ...
- spring异常处理器
一.本篇文章旨在讨论异常处理器: 1.因为异常处理器在实战中通常用来处理开发人员自定义的运行时异常,所以如果要了解如何自定义运行时异常,请自行搜索相关资料. 2.本文的demo用IndexOutOfB ...