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 (ij), 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)的更多相关文章

  1. 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum

    又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...

  2. [LeetCode] 327. Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  3. LeetCode 327. Count of Range Sum

    无意看到的LeetCode新题,不算太简单,大意是给一个数组,询问多少区间和在某个[L,R]之内.首先做出前缀和,将问题转为数组中多少A[j]-A[i] (j>i)在范围内. 有一种基于归并排序 ...

  4. 327. Count of Range Sum

    /* * 327. Count of Range Sum * 2016-7-8 by Mingyang */ public int countRangeSum(int[] nums, int lowe ...

  5. 【LeetCode】327. Count of Range Sum

    题目: Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusiv ...

  6. 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 ...

  7. 327 Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 确认某端口占用情况并结束相应进程(Windows)

    (1)确认某端口是否被占用 (2)通过查找对应的PID号,定位是哪一个进程在使用该端口 (3)通过PID号结束该进程 # 查找端口2000是否被占用C:\Users\tdcqma>netstat ...

  2. c# FastReport开发报表

    本文介绍c#应用FastReport开发报表,因此首先附该工具下载地址:http://download.csdn.net/detail/hws1058648831a/6378499 下载解压后可以直接 ...

  3. VC6.0下string不能用pusk_back,可用+=代替

    2013-09-11 21:14:32 在VS下运行正确的代码,拿到VC6.0下,编译出错,提示: error C2039: 'push_back' : is not a member of 'bas ...

  4. ffmpeg 2.8.1 最新版本 VS2013 可调式动态库

    ffmpeg 2.8.1 最新版本 VS2013 可调式动态库 由于大多数初学者都在想尽各种版本寻求VC编译调试ffmpeg的版本,我也曾经移植过几个版本的ffmpeg到VC上编译.: 链接所需动态库 ...

  5. Hearthstone-Deck-Tracker汉化处理技巧

    https://github.com/chucklu/Hearthstone-Deck-Tracker 首先本地需要有自己的远端chucklu以及作者的远端epix37 $ git remote -v ...

  6. Hbase总结(一)-hbase命令,hbase安装,与Hive的区别,与传统数据库的区别,Hbase数据模型

    Hbase总结(一)-hbase命令 下面我们看看HBase Shell的一些基本操作命令,我列出了几个常用的HBase Shell命令,如下: 名称 命令表达式 创建表 create '表名称', ...

  7. hdu 1559 最大子矩阵 (简单dp)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1559 #include <cstring> #include <cstdlib> ...

  8. 类handler

    /** The handler class is the interface for dynamically loadable storage engines. Do not add ifdefs a ...

  9. 函数os_file_pread

    /*******************************************************************//** Does a synchronous read ope ...

  10. moment 和ko 绑定msdate格式的日期值(静态text)

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...