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 ...
随机推荐
- PHP之APC缓存详细介绍(转)
1.APC缓存简介 APC,全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”.它为我们提供了缓存和优化PHP的中间代码的框架. APC的缓存分两部分:系统缓存和用户数据缓 ...
- Main()方法
C#是从方法Main()开始执行的.这个方法必须是类或结构的静态方法,并且其返回类型必须是int或void .虽然显式指定p山屺修饰符是很常见的,因为按照定义,必须在程序外部调用该方法,但我们给该入口 ...
- CodeForces Round #296 Div.2
A. Playing with Paper 如果a是b的整数倍,那么将得到a/b个正方形,否则的话还会另外得到一个(b, a%b)的长方形. 时间复杂度和欧几里得算法一样. #include < ...
- UVa 1025 (动态规划) A Spy in the Metro
题意: 有线性的n个车站,从左到右编号分别为1~n.有M1辆车从第一站开始向右开,有M2辆车从第二站开始向左开.在0时刻主人公从第1站出发,要在T时刻回见车站n 的一个间谍(忽略主人公的换乘时间).输 ...
- NSAutoReleasePool
做iPhone应用开发已经2年多了, 但一些基础的概念性问题只是大致了解, 脑袋中有个模糊的概念. 虽然对平时工作开发没什么影响, 不过时间长了, 心里总是有点虚. 所以从现在开始, 每当我遇到一个模 ...
- UVa 1153 Keep the Customer Satisfied 【贪心 优先队列】
题意:给出n个工作,已知每个工作需要的时间last,以及截止时间end,(必须在截止时间之前完成)问最多能够完成多少个工作 首先预处理,将这n件任务按照截止时间从小到大排序 然后用一个cur记录当前做 ...
- Asp.Net微信登录-手机网站APP应用
要求:公众号必须先认证,认证费用¥300/年,比较黑 一.微信登录核心代码 //核心代码,没判断异常 1.登录页面 protected void Page_Load(object sender, Ev ...
- PHP运行模式的深入理解
PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli 命令行运行 ( ...
- 利用dns解析来实现网站的负载均衡
当网站的访问量大了就会考虑负载均衡,这也是每一个架构师的基本功了,其基本地位就相当于相声里的说学逗唱,活好不好就看这个了 :) 传统的负载均衡思路是单点的,不管你是硬件的还是软件的基本都是这样的原理 ...
- Test语言编译器V0.8
感觉这个挺好耍的,书上的代码有错误,而且功能有限. 一.词法分析 特点: (1)可对中文进行识别:(2)暂不支持负数,可以在读入‘-'时进行简单标记后就能对简单负数进行识别了. #include &l ...