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 ...
随机推荐
- Django admin的一些有用定制
Model实例,myapp/models.py: from django.db import models class Blog(models.Model): name = models.CharFi ...
- netty 解决TCP粘包与拆包问题(三)
今天使用netty的固定长度进行解码 固定长度解码的原理就是按照指定消息的长度对消息自动解码. 在netty实现中,只需要采用FiexedLengthFrameDecoder解码器即可... 以下是服 ...
- BZOJ 2154 Crash的数字表格
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2154 题意: 思路: i64 mou[N]; void init(int N){ ...
- BZOJ 1898 Swamp 沼泽鳄鱼(矩阵)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1898 题意:一个无向图.给出起点和终点,以及某些时刻某些点不能到达的信息.问从起点出发在 ...
- HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)
Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- Qt之自定义界面(右下角冒泡)
简述 网页右下角上经常会出现一些提示性的信息,桌面软件中也比较常见,类似360新闻.QQ消息提示一样! 这种功能用动画实现起来很简单,这节我们暂时使用定时器来实现,后面章节会对动画框架进行详细讲解. ...
- 为初学者写ORM,ORM的原理及测试案例
提纲 一.什么是ORM.二.反射以及Attribute在ORM中的应用.三.创建一个数据库表和表对应的实体model.四.实体model如何映射出数据库表.五.组合ORM映射生成insert语句.六. ...
- POJ 2752 (KMP 所有可能长度的前缀后缀) Seek the Name, Seek the Fame
题意: 求一个字符串的相同前缀后缀的所有可能的长度,这里该字符串其本身也算自己的前缀和后缀. 分析: 我们知道next数组的性质是,该字符之前的字符串的最大相同前缀后缀. 既然知道了最大的,即next ...
- attachEvent,addEventListener事件绑定
兼容各主流浏览器的事件绑定(在同一个事件上添加多个处理函数). 1.绑定方法: //IE attachEvent(事件名, 函数) oBtn.attachEvent('onclick', aaa); ...
- 永久的CheckBox(单选,全选/反选)!
<html> <head> <title>选择</title> <script type="text/javascript" ...