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:

Input: nums = [2, 5, -1], lower = -2, upper = 2,
Output: 3
Explanation: The three ranges are : [0, 0], [2, 2], [0, 2] and their respective sums are: -2, -1, 2.
 

Approach #1: C++.

class Solution {
public:
int countRangeSum(vector<int>& nums, int lower, int upper) {
int len = nums.size();
if (len == 0) return 0;
vector<long> sum(len+1, 0);
for (int i = 0; i < len; ++i)
sum[i+1] += sum[i] + nums[i];
return mergeSort(sum, lower, upper, 0, len+1);
} private:
int mergeSort(vector<long>& sum, int lower, int upper, int left, int right) {
if (right - left <= 1) return 0;
int mid = left + (right - left) / 2;
int m = mid, n = mid, count = 0;
count = mergeSort(sum, lower, upper, left, mid) + mergeSort(sum, lower, upper, mid, right);
for (int i = left; i < mid; ++i) {
while (m < right && sum[m] - sum[i] < lower) m++;
while (n < right && sum[n] - sum[i] <= upper) n++;
count += n - m;
}
inplace_merge(sum.begin()+left, sum.begin()+mid, sum.begin()+right);
return count;
}
};

  

Approach #2: Java.

class Solution {
public int countRangeSum(int[] nums, int lower, int upper) {
if (nums == null || nums.length == 0) return 0;
long[] sums = new long[nums.length];
long sum = 0;
for (int i = 0; i < nums.length; ++i) {
sum += nums[i];
sums[i] += sum;
}
return mergeSort(sums, lower, upper, 0, nums.length-1);
} private int mergeSort(long[] sums, int lower, int upper, int left, int right) {
if (right < left) return 0;
else if (left == right) {
if (sums[left] >= lower && sums[right] <= upper) return 1;
else return 0;
}
int mid = left + (right - left) / 2;
int count = mergeSort(sums, lower, upper, left, mid) + mergeSort(sums, lower, upper, mid+1, right);
int m = mid+1, n = mid+1;
for (int i = left; i <= mid; ++i) {
while (m <= right && sums[m] - sums[i] < lower) m++;
while (n <= right && sums[n] - sums[i] <= upper) n++;
count += n - m;
}
mergeHelper(sums, left, mid, right);
return count;
} private void mergeHelper(long[] sums, int left, int mid, int right) {
int i = left;
int j = mid + 1;
long[] copy = new long[right-left+1];
int p = 0;
while (i <= mid && j <= right) {
if (sums[i] < sums[j]) {
copy[p++] = sums[i++];
} else {
copy[p++] = sums[j++];
}
} while (i <= mid) {
copy[p++] = sums[i++];
} while (j <= right) {
copy[p++] = sums[j++];
} System.arraycopy(copy, 0, sums, left, right-left+1);
}
}

  

Approach #3: Python.

class Solution(object):
def countRangeSum(self, nums, lower, upper):
"""
:type nums: List[int]
:type lower: int
:type upper: int
:rtype: int
"""
first = [0]
for num in nums:
first.append(first[-1] + num) def sort(lo, hi):
mid = (lo + hi) / 2
if mid == lo:
return 0
count = sort(lo, mid) + sort(mid, hi)
i = j = mid
for left in first[lo:mid]:
while i < hi and first[i] - left < lower: i += 1
while j < hi and first[j] - left <= upper: j += 1
count += j - i
first[lo:hi] = sorted(first[lo:hi])
return count
return sort(0, len(first))

  

Notes:

C++ -----> inplace_merge

default (1)
template <class BidirectionalIterator>
void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle,
BidirectionalIterator last);
custom (2)
template <class BidirectionalIterator, class Compare>
void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle,
BidirectionalIterator last, Compare comp);
Merge consecutive sorted ranges

Merges two consecutive sorted ranges: [first,middle) and [middle,last), putting the result into the combined sorted range [first,last).

The elements are compared using operator< for the first version, and comp for the second. The elements in both ranges shall already be ordered according to this same criterion (operator< or comp). The resulting range is also sorted according to this.

The function preserves the relative order of elements with equivalent values, with the elements in the first range preceding those equivalent in the second.

for example:

// inplace_merge example
#include <iostream> // std::cout
#include <algorithm> // std::inplace_merge, std::sort, std::copy
#include <vector> // std::vector int main () {
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10);
std::vector<int>::iterator it; std::sort (first,first+5);
std::sort (second,second+5); it=std::copy (first, first+5, v.begin());
std::copy (second,second+5,it); std::inplace_merge (v.begin(),v.begin()+5,v.end()); std::cout << "The resulting vector contains:";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return 0;
}

  

output:

The resulting vector contains: 5 10 10 15 20 20 25 30 40 50

  

327. Count of Range Sum(inplace_marge)的更多相关文章

  1. 327. Count of Range Sum

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

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

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

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

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

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

  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 区间和计数

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

  7. LeetCode 327. Count of Range Sum

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

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

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

  9. LeetCode Count of Range Sum

    原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...

随机推荐

  1. 观察OnPaint与OnIdle与OnSize事件

    import wx class SketchWindow(wx.Window): def __init__(self, parent, ID): wx.Window.__init__(self, pa ...

  2. 【linux】top更改排序顺序

    top更改排序顺序的方式有很多,这里介绍两个比较简单使用的. 1,快捷键: 大写M:根据内存排序,默认从大到小,大写R更改为从小到大排序 大写P:根据CPU使用排序,默认从大到小,大写R更改为从小到大 ...

  3. LeetCode:N叉树的最大深度【559】

    LeetCode:N叉树的最大深度[559] 题目描述 给定一个N叉树,找到其最大深度. 最大深度是指从根节点到最远叶子节点的最长路径上的节点总数. 例如,给定一个 3叉树 : 我们应返回其最大深度, ...

  4. Ruby操作数据库基本步骤

    1.rails g model university name:string 2.model has_many :colleges belongs_to :university has_one :us ...

  5. 【转】数据存储——APP 缓存数据线程安全问题探讨

    http://blog.cnbang.net/tech/3262/ 问题 一般一个 iOS APP 做的事就是:请求数据->保存数据->展示数据,一般用 Sqlite 作为持久存储层,保存 ...

  6. 20145239杜文超 《Java程序设计》实验二 Java面向对象程序设计实验报告

    20145239 <Java程序设计>实验二 Java面向对象程序设计实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S. ...

  7. 深入理解JVM - Java内存区域与内存溢出异常 - 第二章

    一 运行时数据区域 JVM在执行Java程序的过程中会把它管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以及创建和销毁的时间. 程序计数器 程序计数器(Program Counter ...

  8. 京东面试题 Java相关

    1.JVM的内存结构和管理机制: JVM实例:一个独立运行的java程序,是进程级别 JVM执行引擎:用户运行程序的线程,是JVM实例的一部分 JVM实例的诞生 当启动一个java程序时.一个JVM实 ...

  9. 机器学习 Regularization and model selection

    Regularization and model selection 假设我们为了一个学习问题尝试从几个模型中选择一个合适的模型.例如,我们可能用一个多项式回归模型hθ(x)=g(θ0+θ1x+θ2x ...

  10. java面试题07

    1.重载和重写的区别? 重载(Overload):(1)方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载Overloading是一个类中多态性 ...