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 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
.
class pair {
public int idx;
public long val;
public pair(int idx, long val) {
super();
this.idx = idx;
this.val = val;
} } class pairComparator implements Comparator { public int compare(Object o1, Object o2) {
pair p1 = (pair) o1;
pair p2 = (pair) o2;
if(p1.val < p2.val) {
return -1;
} else {
return 1;
}
} } public class Solution { public static ArrayList<pair> toSortedList(long[] sum) { ArrayList<pair> ls = new ArrayList<pair> ();
for(int i=0; i<sum.length; ++i) {
pair p = new pair(i, sum[i]);
ls.add(p);
}
Collections.sort(ls, new pairComparator());
return ls;
} public static int binarySearch(ArrayList<pair> ls, int l, int r, long lb, long ub, int index) { if(l > r) {
return 0;
}
if(l == r) {
if(ls.get(l).val >= lb && ls.get(l).val <= ub && ls.get(l).idx >= index) {
//System.out.println("candidate index range: [" + index + ", " + ls.get(l).idx + "]");
return 1;
}
return 0;
} int rs = 0; int mid = (l + r) / 2;
if(ls.get(mid).val < lb) {
rs = binarySearch(ls, mid+1, r, lb, ub, index);
} else if(ls.get(mid).val > ub) {
rs = binarySearch(ls, l, mid-1, lb, ub, index);
} else {
rs = binarySearch(ls, l, mid-1, lb, ub, index) + binarySearch(ls, mid+1, r, lb, ub, index);
if(ls.get(mid).idx >= index) {
//System.out.println("candidate index range: [" + index + ", " + ls.get(l).idx + "]");
rs++;
}
} return rs;
} public int countRangeSum(int[] nums, int lower, int upper) { int n = nums.length;
if(n == 0) {
return 0;
} long[] sum = new long[n];
sum[0] = nums[0];
for(int i=1; i<n; ++i) {
sum[i] = sum[i-1] + nums[i];
} int rs = 0;
ArrayList<pair> ls = toSortedList(sum);
for(int i=0; i<n; ++i) {
long new_lower = (long)lower + sum[i] - (long)nums[i];
long new_upper = (long)upper + sum[i] - (long)nums[i];
int count = binarySearch(ls, 0, ls.size()-1, new_lower, new_upper, i);
rs += count;
} return rs;
}
}
leetcode@ [327] Count of Range Sum (Binary Search)的更多相关文章
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- [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
无意看到的LeetCode新题,不算太简单,大意是给一个数组,询问多少区间和在某个[L,R]之内.首先做出前缀和,将问题转为数组中多少A[j]-A[i] (j>i)在范围内. 有一种基于归并排序 ...
- 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
题目: 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 ...
- 327 Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
随机推荐
- JDBC学习总结(二)
1.JDBC的基本应用 1)创建数据库: create database test; use test; 2)创建表: create table student( id int(4) no ...
- JS代码片段:appendHTML 和 insertAdjacentHTML
function appendHTML(container,html,position){ position =position || 'after'; var objE = document.cre ...
- php库Faker
Faker License : MIT Source Code Allo点评:Faker是一个很神奇的项目,会自动生成拟真的数据,包括用户资料.长文本.IP.日期等等,在网站上线前测试时非常好用. g ...
- JNI读取assets资源文件
源自:http://www.rosoo.net/a/201112/15459.html assets目录底下的文件会被打包到一个apk文件里,这些资源在安装时他们并没被解压,使用时是直接从apk中读取 ...
- android4.4内核移植
01 init/目录下Kconfig修改: 956行添加: config PANIC_TIMEOUT int "Default panic timeout" help Set de ...
- 在BSP的.bat文件下設置全局變量方法
用于多個產品共用一個BSP的時候,在BSP的.bat文件中設置全局變量,去掉不需要加載的驅動和不同點是很好的方法. 一,舉例:BSP中.bat的一段code: set BSP_SMDK2443=1 s ...
- C++中变量自动初始化的问题
C++中有一些变量在如果没有赋初值会被编译器自动赋值为0,但有的变量又不会这样,而得到一个随机数,下面具体讨论一下: 首先看一下C++中的几个存储区:1.栈区:由编译器自动分配释放 ,存放函数的参数值 ...
- JAVA使用apache http组件发送POST请求
在上一篇文章中,使用了JDK中原始的HttpURLConnection向指定URL发送POST请求 可以看到编码量有些大,并且使用了输入输出流 传递的参数还是用“name=XXX”这种硬编的字符串进行 ...
- Qt之QTableView添加复选框(QAbstractItemDelegate)
简述 上节分享了使用自定义模型QAbstractTableModel来实现复选框.下面我们来介绍另外一种方式: 自定义委托-QAbstractItemDelegate. 简述 效果 QAbstract ...
- UVa 10088 (Pick定理) Trees on My Island
这种1A的感觉真好 #include <cstdio> #include <vector> #include <cmath> using namespace std ...