327. Count of Range Sum(inplace_marge)
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> |
---|---|
custom (2) |
template <class BidirectionalIterator, class Compare> |
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)的更多相关文章
- 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
又是一道有意思的题目,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 (Binary Search)
https://leetcode.com/problems/count-of-range-sum/ Given an integer array nums, return the number of ...
- 【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 区间和计数
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)在范围内. 有一种基于归并排序 ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- LeetCode Count of Range Sum
原题链接在这里:https://leetcode.com/problems/count-of-range-sum/ 题目: Given an integer array nums, return th ...
随机推荐
- 相比ICO,DAICO主要有这两方面优势
都说ICO已死,很有一部分人对无币区块链持保留态度,自从V神提出DAICO一来,大家似乎看到了新的方向,不少项目围绕其展开.那对比ICO,DAICO有哪些优势呢?主要是以下两点: DAICO维护了投资 ...
- python mmap使用记录
1.写文件 with open('??', 'r+b') as f: with contextlib.closing(mmap.mmap(f.fileno(), size, flags=mmap.MA ...
- Wannafly挑战赛12 B T95要减肥 【贪心】
链接:https://www.nowcoder.com/acm/contest/79/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- zabbix增加服务器tcp监控
zabbix server web界面,需要导入 tcp 监控模板 操作步骤: Configuration --> Templates --> Import ,选择 本地的 zb ...
- python列表切片
Python中符合序列的有序序列都支持切片(slice),例如列表,字符串,元组. 格式:[start:end:step] start:起始索引,从0开始,-1表示结束 end:结束索引 step:步 ...
- python的上下文管理器-1
reference:https://zhuanlan.zhihu.com/p/26487659 来看看如何正确关闭一个文件. 普通版: def m1(): f = open("output. ...
- 分享知识-快乐自己:2017IDEA破解教程
首先 修改host文件: 文件路径:C:\Windows\System32\drivers\etc\hosts 修改:将“0.0.0.0 account.jetbrains.com”追加到hosts文 ...
- openfire插件开发环境
创建java工程 SamplePlugin: package com.hoo.server.plugin; import java.io.File; import org.jivesoftware.o ...
- Servlet_03_部署描述符
二.参考文档 1.Servlet 3.0 之 部署描述符 2.web.xml配置详解 部署描述符文件
- TestDescription文档描述测试过程
测试描述文档是用xml语言描述测试过程的文档,一个测试过程包括测试信号建立,UUT引脚确定,建立连接关系,数据测量,断开连接关系,复位测试信号等步骤. 下图用标准的ATML语言描述了接通直流电源并测量 ...