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 ...
随机推荐
- swift:类型转换(is用作判断检测、as用作类型向下转换)
类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式. 类型转换在Swift中使用is 和 as操作符实现.这两个操作符提供了一种简单达意的方式去检查值的类型或者转换 ...
- Android ActionBar标题和渐变背景
需要在AndroidManifest.xml中设置 android:theme="@style/Theme.AppCompat" 如果提示找不到,请按下图设置: 至于如何引入的方法 ...
- 如何学习一个新的PHP框架
如今的PHP框架层出不穷,我不是这方面的专家,甚至不能熟练地使用其中的一种,所以我不做推荐,也不想讨论哪些算是框架哪些不算框架.这里我要讨论的是如何才能更快地开始使用某个新的框架. 首先你当然必须选择 ...
- android下activity中多个listview只允许主界面滚动
之前发现了自己的APP在处理两个listview时产生的一个bug.当两个listview中的item数量多出手机屏幕时,listview不能显示完全.一开始觉得只要加一个scrollview就可以了 ...
- Xcode插件开发
一.安装模板 1.git clone https://github.com/kattrali/Xcode-Plugin-Template.git 2.cd Xcode-Plugin-Template ...
- Tornado 中的 get() 或 post() 方法
---恢复内容开始--- Tornado 中的 get() 或 post() 方法 请求处理程序和请求参数 Tornado 的 Web 程序会将 URL 或者 URL 范式映射到 tornado.we ...
- BZOJ 2339 卡农(组合数学)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2339 题意: 思路: i64 Pow(i64 a,i64 b,i64 mod){ ...
- highcharts 饼图显示数据比例如何保留二位小数
var NewPerCent=parseFloat(NewPerCent.toString()).toFixed(2);return '<b>'+ this.point.name +'&l ...
- UVALive 4128 Steam Roller(最短路(拆点,多状态))
题意:模拟了汽车的行驶过程,边上的权值为全速通过所消耗的时间,而起步(从起点出发的边).刹车(到终点结束的边).减速(即将拐弯的边).加速(刚完成拐弯的边)这四种不能达到全速的情况,消耗的时间为权值* ...
- [反汇编练习]160个CrackMe之001
[反汇编练习] 160个CrackMe之001. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...