BIT-Count of Range Sum
2019-12-17 18:56:56
问题描述:

问题求解:
本题个人感觉还是很有难度的,主要的难点在于如何将题目转化为bit计数问题。
首先构建一个presum数组,这个没有问题。
需要对于任意一个j,我们需要知道的是presum[i]的个数使得 lower <= presum[j] - presum[i] <= upper。
上述等式等价于求符合 presum[j] - upper <= presum[i] <= presum[j] - lower 的presum[i]的个数。
看到这个是不是有点眼熟了呢,求满足某个条件的区间的个数,这个和逆序数问题是完全一致的,只是在那个问题里我们只有一个上界,这里多了一个下界罢了。
同逆序数的解法,这里我们可以使用树状数组来进行求解。
public int countRangeSum(int[] nums, int lower, int upper) {
if (nums.length == 0) return 0;
int res = 0;
int n = nums.length;
long[] presum = new long[n];
presum [0] = nums[0];
for (int i = 1; i < n; i++) presum[i] = presum[i - 1] + nums[i];
long[] copy = Arrays.copyOf(presum, n);
Arrays.sort(copy);
TreeMap<Long, Integer> map = new TreeMap<>();
int rank = 0;
for (int i = 0; i < n; i++) {
if (i == 0 || copy[i] != copy[i - 1]) {
map.put(copy[i], ++rank);
}
}
int[] bit = new int[map.size() + 1];
for (int i = 0; i < n; i++) {
if (presum[i] >= lower && presum[i] <= upper) res += 1;
Long r = map.floorKey(presum[i] - lower);
Long l = map.ceilingKey(presum[i] - upper);
if (l != null && r != null) res += query(bit, map.get(r)) - query(bit, map.get(l) - 1);
update(bit, map.get(presum[i]));
}
return res;
}
private void update(int[] bit, int idx) {
for (int i = idx; i < bit.length; i += i & -i) {
bit[i] += 1;
}
}
private int query(int[] bit, int idx) {
int res = 0;
for (int i = idx; i > 0; i -= i & -i) {
res += bit[i];
}
return res;
}
BIT-Count of Range Sum的更多相关文章
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- 327. Count of Range Sum
/* * 327. Count of Range Sum * 2016-7-8 by Mingyang */ public int countRangeSum(int[] nums, int lowe ...
- [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 ...
- 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 ...
- [Swift]LeetCode327. 区间和的个数 | 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(inplace_marge)
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- Leetcode327: Count of Range Sum 范围和个数问题
###问题描述 给定一个整数数组,返回range sum 落在给定区间[lower, upper] (包含lower和upper)的个数.range sum S(i, j) 表示数组中第i 个元素到j ...
- [LeetCode] 327. Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
随机推荐
- 查漏补缺:进程间通信(IPC):FIFO
1.FIFO FIFO,又称命名管道.不同于pipe管道的只能用于拥有共同祖先进程的两个进程间通信,因FIFO通过路径绑定,所以即使是不相关的进程间也可通过FIFO进行数据交换. FIFO是一种文件类 ...
- Autotestplat.com 更新了!
1 提供测试发帖留言功能 2 自动化平台体验功能 3 提供招聘资讯功能 4 提供推荐书籍功能
- sed 分组替换
将文件以help开头的句子前加# [root@localhost]# cat a.txthelp b helphelp1helphelp2help c help[root@localhost]# se ...
- Java入门教程九(封装继承多态)
封装 封装就是将对象的属性和方法相结合,通过方法将对象的属性和实现细节保护起来,实现对象的属性隐藏.做法就是:修改属性的可见性来限制对属性的访问,并为每个属性创建一对取值(getter)方法和赋值(s ...
- Tornado 简述
前言 python 旗下,群英荟萃,豪杰并起.单是用于 web 开发的,就有 webpy.web2py.bottle.pyramid.zope2.flask.tornado.django 等等,不一而 ...
- 什么是x86什么是x64 它们有什么区别
1.内存寻址不同: 32位系统,最大支持3.5G内存,如果在32位系统中使用4G或更大的内存,电脑最多只可以识别3.4G左右可用,而64位系统最大可以支持128G大内存. 2.运算速度不同: 64位系 ...
- 大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式)
大厂面试官最常问的@Configuration+@Bean(JDKConfig编程方式) 现在大部分的Spring项目都采用了基于注解的配置,采用了@Configuration 替换标签的做法.一 ...
- 一起了解 .Net Foundation 项目 No.13
.Net 基金会中包含有很多优秀的项目,今天就和笔者一起了解一下其中的一些优秀作品吧. 中文介绍 中文介绍内容翻译自英文介绍,主要采用意译.如与原文存在出入,请以原文为准. MVVM Light To ...
- 前端Bug解决方案
没错!我正在写bug呢!不管你是小白还是大牛,写bug无可避免,遇到bug怎么办?别慌!毛主席教导我们"战略上藐视BUG,战术上重视BUG"!前端遇到的bug无非就三个方面结构层( ...
- 【学习笔记】Golang学习方向整理
前言 作为一个Java开发,给大家说Golang方向,好吓人...溜了溜了... 哦对了,如有不对的地方,还请指出.感谢! 某面试平台golang技能要求简要摘录 掌握 GO 语言,熟悉常用 pack ...