LC327 Count of Range Number
这一题,我们使用了分治法。
首先看时间复杂度为o(n^2),比较naïve的方法:
使用一个数组sum[],长度为原数组长度+1,每一个元素sum[i]为原数组中index0~i-1之和,显然当sum[j] – sum[i]就是i~j-1之和,于是我们只需要两个for来遍历所有[i, j],并且比较是否在lower~upper之间即可。
但是题目要求时间复杂度由于o(n^2),考虑使用分治
1) 分:将sum[]分为左右两部分,分别计算满足条件的[i,j]
2) 合:除了i,j都在左或者都在右,还有一种情况,i在左,j在右 想要复杂度为o(nlogn),由主方法易知,合这一步必须为线性复杂度。这里有一个小trick,在合这一步中我们对这一次迭代的sum[start]-sum[end]进行排序,这样,返回之后,对于上一层函数来说,左右两部分都已经排好了序,我们只需要检测i = start~mid,mid<j,k<=end然后找到第一个sum[k]-sum[i] > lower, 和第一个sum[j] – sum[i] > upper,然后j-k就是满足条件的范围的个数。
代码如下:
- class Solution {
- public int countRangeSum(int[] nums, int lower, int upper) {
- long[] sum = new long[nums.length+1];
- for(int i=0; i<nums.length; i++){
- sum[i+1] = sum[i] + nums[i];
- }
- return mergeSort(sum, 0, nums.length + 1, lower, upper);
- }
- private int mergeSort(long[] sum, int start, int end, int lower, int upper){
- if(end - start <= 1)
- return 0;
- int mid = start + (end - start) / 2;
- //int mid = (start + end) / 2;
- int count = mergeSort(sum, start, mid, lower, upper) + mergeSort(sum, mid, end, lower, upper);
- long[] tmp = new long[end - start];
- int j = mid, k = mid, t = mid;
- for(int i = start, r = 0; i < mid; i++, r++){
- while(k < end && sum[k] - sum[i] < lower)
- k++;
- while(j < end && sum[j] - sum[i] <= upper)
- j++;
- while(t < end && sum[t] < sum[i])
- tmp[r++] = sum[t++];
- tmp[r] = sum[i];
- count += j - k;
- }
- System.arraycopy(tmp, 0, sum, start, t - start);
- return count;
- }
- }
LC327 Count of Range Number的更多相关文章
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- Lintcode: Count of Smaller Number
Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...
- LeetCode "Count of Smaller Number After Self"
Almost identical to LintCode "Count of Smaller Number before Self". Corner case needs to b ...
- Lintcode249 Count of Smaller Number before itself solution 题解
[题目描述] Give you an integer array (index from 0 to n-1, where n is the size of this array, data value ...
- 327. Count of Range Sum
/* * 327. Count of Range Sum * 2016-7-8 by Mingyang */ public int countRangeSum(int[] nums, int lowe ...
- [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 ...
- 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 ...
随机推荐
- <转>http协议 文件下载原理详解
最近研究了一下关于文件下载的相关内容,觉得还是写些东西记下来比较好.起初只是想研究研究,但后来发现写个可重用性比较高的模块还是很有必要的,我想这也是大多数开发人员的习惯吧. 对于HTTP协议,向服务器 ...
- 2018-2-13-安装visualStudio-出现-cant-install-Microsoft.TeamFoundation.OfficeIntegration.Resources...
title author date CreateTime categories 安装visualStudio 出现 cant install Microsoft.TeamFoundation.Offi ...
- jQuery.event.move
http://stephband.info/jquery.event.move/ Move events movestart Fired following touchmove or mousemov ...
- 廖雪峰Java12maven基础-2maven进阶-1使用插件
1.maven的Lifecycle,Phase和Goal: 使用maven构建项目就是执行Lifecycle 执行Lifecycle就是按顺序执行一系列Phase 每执行一个Phase,都会执行该Ph ...
- 第十四章 Odoo 12开发之部署和维护生产实例
本文中将学习将 Odoo 服务器作为生产环境的基本准备.安装和维护服务器是一个复杂的话题,应该由专业人员完成.本文中所学习的不足以保证普通用户创建应对包含敏感数据和服务的健壮.安全环境. 本文旨在介绍 ...
- 简单科普下hosts文件原理与制作
简单科普下hosts文件原理与制作 hosts文件是一个用于储存计算机网络中各节点信息的计算机文件.这个文件负责将主机名映射到相应的IP地址.hosts文件通常用于补充或取代网络中DNS的功能.和DN ...
- JEECMS 自定义标签
CMS 是”Content Management System” 的缩写,意为” 内容管理系统”. 内容管理系统是企业信息化建设和电子政务的新宠,也是一个相对较新的市场.对于内容管理,业界还没有一个统 ...
- NAT后的FTP服务器部署笔记
(2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2017年2月5日) 寒假开始以后,过年之前有一个任务,为实验室的人搭建一个FTP,用之前部署好的物理服务器.这本就是网管干 ...
- java调js基础
public static void main(String[] args)throws Exception { ScriptEngine se = new ScriptEngineManager() ...
- springboot2配置druid数据库连接池
注意配置以下的依赖: <!-- 引入druid数据源--> <dependency> <groupId>com.alibaba</groupId> &l ...