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.
详见:https://leetcode.com/problems/count-of-range-sum/description/
class Solution {
public:
int countRangeSum(vector<int>& nums, int lower, int upper)
{
vector<long> sums(nums.size() + 1, 0);
for (int i = 0; i < nums.size(); ++i)
{
sums[i + 1] = sums[i] + nums[i];
}
return countAndMergeSort(sums, 0, sums.size(), lower, upper);
}
int countAndMergeSort(vector<long> &sums, int start, int end, int lower, int upper)
{
if (end-start<=1)
{
return 0;
}
int mid = start + (end - start) / 2;
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, 0);
for (int i = start, r = 0; 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;
}
};
参考:https://www.cnblogs.com/grandyang/p/5162678.html
327 Count of Range Sum 区间和计数的更多相关文章
- [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] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- 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
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- 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
题目: 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 327. Count of Range Sum
无意看到的LeetCode新题,不算太简单,大意是给一个数组,询问多少区间和在某个[L,R]之内.首先做出前缀和,将问题转为数组中多少A[j]-A[i] (j>i)在范围内. 有一种基于归并排序 ...
- [Swift]LeetCode327. 区间和的个数 | Count of Range Sum
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
随机推荐
- Ubuntu挂载硬盘,修改卷标
Ubuntu挂载硬盘,修改卷标转载2016-03-06 17:03:21标签:ubuntu Ubuntu不像windows,硬盘插入电脑不会自动读取硬盘 数据,需要把硬盘挂载到文件夹上,然后才能访问硬 ...
- 2.8 补充:shell变量引用方式
一 变量 变量:本质上是程序中保存用户数据的一块内存空间,变量名就是内存空间地址. Shell中:变量可由字母数字和下划线组成,以字母或下划线开头. 命名:PATH=/sbin ...
- 【Codeforces 1019A】Elections
[链接] 我是链接,点我呀:) [题意] 每个人都有自己喜欢的队员 但是如果贿赂他们可以让他们更改自己喜欢的队员 问你最少要花多少钱贿赂队员才能让1号队员严格有最多的人喜欢? [题解] 除了1号之外, ...
- MaxScale初探
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://mrcto.blog.51cto.com/1923168/1437287 内容预览 ...
- Mutual Training for Wannafly Union #5
A(UVA12336) 题意:给一个n*m(n,m<=1e5)的棋盘,棋盘上有一些障碍点不能放棋子,现在要在棋盘上放4个棋子,满足A->B->C->D->A,其中走的规则 ...
- [codevs 1961]躲避大龙(dfs)
题目:http://dev.codevs.cn/problem/1961/ 分析: 被“SPFA”的标签骗了…… 看了hzwer的博客才知道可以用f[i][0..60]表示每个点每个秒是否可以到.至于 ...
- 27、Java并发性和多线程-CAS(比较和替换)
以下内容转自http://ifeve.com/compare-and-swap/: CAS(Compare and swap)比较和替换是设计并发算法时用到的一种技术.简单来说,比较和替换是使用一个期 ...
- 第6章 TCP/IP路由协议故障处理
第6章 TCP/IP路由协议故障处理 一.缺省网关 当包的目的地址不在路由器的路由表中,如路由器配置了缺省网关,则转发到缺省网关,否则就丢弃. Show ip route :查看Cisco路由器的缺省 ...
- 读书笔记-APUE第三版-(7)进程环境
本章关注单进程执行环境:启动&终止.參数传递和内存布局等. 进程启动终止 如图所看到的: 启动:内核通过exec函数执行程序,在main函数执行之前.会调用启动例程(start-up rout ...
- 【转】 vsftp上传文件出现553 Could not create file解决方法
因工作需要,需要搭建一个ftp服务器,我使用ubuntu 10.04操作系统,下载vsftpdy源代码, 进行了编译,安装,然后按照INSTALL文件,创建了用户等操作. 因为时间比较紧,我采用 ...