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 ...
随机推荐
- Python3爬虫:(一)爬取拉勾网公司列表
人生苦短,我用Python 爬取原因:了解一下Python工程师在北上广等大中城市的薪资水平与入职前要求. Python3基础知识 requests,pyquery,openpyxl库的使用 爬取前的 ...
- CSS中网格布局实战(初级)
大家好,网格布局是我们在网页布局中经常用到的,那这里我就给大家分享一篇简单的网格布局,让大家能简单明了的了解网格布局的基本内容.闲话不多说,直接进入主题! 第一步,基本的框架结构.这里直接一个div来 ...
- LeetCode python实现题解(持续更新)
目录 LeetCode Python实现算法简介 0001 两数之和 0002 两数相加 0003 无重复字符的最长子串 0004 寻找两个有序数组的中位数 0005 最长回文子串 0006 Z字型变 ...
- 怎么用Python写一个三体的气候模拟程序
首先声明一下,这个所谓的三体气候模拟程序还是很简单的,没有真的3D效果或数学模型之类的,只不过是一个文字表示的模拟程序.该程序的某些地方可能不太严谨,所以也请各位多多包涵. 所谓三体气候模拟,就是将太 ...
- MVC01
1.Controller 1) 添加: 在Controller目录右键进行添加,出现很多模式供选择,选择空的Controller,命名后新建.新建后Views 目录将同步生成相应名称的视图文件目录 均 ...
- Redis系列五 - 哨兵、持久化、主从
问:骚年,都说Redis很快,那你知道这是为什么吗? 答:英俊潇洒的面试官,您好.我们可以先看一下 关系型数据库 和 Redis 本质上的区别. Redis采用的是基于内存的,采用的是单进程单线程模型 ...
- WEB渗透 - XSS
听说这个时间点是人类这种生物很重要的一个节点 cross-site scripting 跨站脚本漏洞 类型 存储型(持久) 反射型(非持久) DOM型 利用 先检测,看我们输入的内容是否有返回以及有无 ...
- Spark入门(七)--Spark的intersection、subtract、union和distinc
Spark的intersection intersection顾名思义,他是指交叉的.当两个RDD进行intersection后,将保留两者共有的.因此对于RDD1.intersection(RDD2 ...
- 把读取sql的结果写入到excel文件
1.利用pandas模块 # encoding: utf-8 import time import pandas as pd import pymysql def getrel(sql): ''' 连 ...
- Java安装和配置
一. Java安装和配置 1.JDK下载地址: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-21331 ...