Description

Given an positive integer array A and an interval. Return the number of subarrays whose sum is in the range of given interval.

Subarray is a part of origin array with continuous index.

Example

Example 1:

Input: A = [1, 2, 3, 4], start = 1, end = 3
Output: 4
Explanation: All possible subarrays: [1](sum = 1), [1, 2](sum = 3), [2](sum = 2), [3](sum = 3).

Example 2:

Input: A = [1, 2, 3, 4], start = 1, end = 100
Output: 10
Explanation: Any subarray is ok.

思路:

O(N) 的两根指针的算法

其实需要三根指针, 因为需要额外记录一下从哪个位置开始的加和已经 >= start 了.

对于每一个左端点 left, 求出对应的两个右端点 right_start, right_end. 前者表示最左边的使得 [left, right_start] 子数组的和不小于 start 的点, 而后者表示最右边的使得 [left, right_end] 子数组的和不大于 end 的点.

right_end - right_start + 1 就是以 left 为左端点的合法子数组的数量.

从左到右枚举 left, 而 right_start, right_end 随着left的增长也是只增不减的, 所以时间复杂度是 O(N)

O(NlogN) 的二分法

求出一个前缀和数组, 然后对于每一个前缀和 presum[right], 我们要求出两个点 left_start, left_end. 前者表示最左边的使得 [left_start, right] 子数组和不大于 end 的点, 后者表示最右边的使得 [left_end, right] 子数组和不小于 start 的点.

枚举 right, 我们可以在 presum[0..right] 上二分查找确定 left_start, left_end.

总结

上面两种方法是相通的, 都是对于子数组的一个端点, 确认另外一个端点的范围. 在枚举其中一个端点的过程, 另外一个端点的范围是单调的, 所以可以使用两根指针 O(N) 地完成.

可以把两根指针和二分法综合一下, 不过这样理论时间复杂度是不变的, 没有很大的必要. 以上两种方法推荐第一种, 复杂度更低.

public class Solution {
/**
* @param A: An integer array
* @param start: An integer
* @param end: An integer
* @return: the number of possible answer
*/
public int subarraySumII(int[] A, int start, int end) {
int n = A.length;
if (n == 0) {
return 0;
} int[] sum = new int[n + 1];
int i, l, r, res = 0;
sum[0] = 0;
for (i = 1; i <= n; ++i) {
sum[i] = sum[i - 1] + A[i - 1];
} l = r = 0;
for (i = 1; i <= n; ++i) {
while (l < i && sum[i] - sum[l] > end) {
++l;
} while (r < i && sum[i] - sum[r] >= start) {
++r;
} res += r - l;
} return res;
}
}

  

Subarray Sum II的更多相关文章

  1. [LintCode] Subarray Sum & Subarray Sum II

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  2. Continuous Subarray Sum II(LintCode)

    Continuous Subarray Sum II   Given an circular integer array (the next element of the last element i ...

  3. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  4. Continuous Subarray Sum II

    Description Given an circular integer array (the next element of the last element is the first eleme ...

  5. LintCode "Subarray Sum II"

    Sliding window doesn't work. So it is a typical partial_sum base solution. As below. However if you ...

  6. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  7. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  8. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

随机推荐

  1. netcore 版本 切换 sdk

    https://docs.microsoft.com/zh-cn/dotnet/core/tools/global-json

  2. 洛谷P3984-数据结构 题解

    题面 这题精,真的精 前言:不要被题目背景和描述误导! Solution: 题目大意 给定一段序列,请你做到区间修改和区间询问. 区间询问即 在 \(L\) 到 \(R\) 区间内,乘上下标后取模的值 ...

  3. Vue框架(四)——路由跳转、路由传参、cookies、axios、跨域问题、element-ui模块

    路由跳转 三种方式: $router.push / $router.go / router-link to this.$router.push('/course'); this.$router.pus ...

  4. Python之路【第二十篇】:python项目之旧版抽屉新热榜

    旧版抽屉新热榜 代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...

  5. git 学习笔记 -- 创建标签

    在Git中打标签非常简单,首先,切换到需要打标签的分支上: $ git branch * dev master $ git checkout master Switched to branch 'ma ...

  6. java枚举enum总结大全

    1.注意点 (1)枚举中的构造方法必须是private的. (2)枚举中可以定义抽象方法和一般方法,但枚举对象必须实现所有抽象方法. (3)枚举对象必须放在第一行. package classTwo0 ...

  7. mysql数据库的安装和连接测试并给root用户赋密码

    一.mysql数据库的安装 Windows下MySQL的配置 以 MySQL 5.1 免安装版为例, 下载 mysql-noinstall-5.1.69-win32.zip ( 官方下载页: http ...

  8. 易百教程人工智能python修正-人工智能无监督学习(聚类)

    无监督机器学习算法没有任何监督者提供任何指导. 这就是为什么它们与真正的人工智能紧密结合的原因. 在无人监督的学习中,没有正确的答案,也没有监督者指导. 算法需要发现用于学习的有趣数据模式. 什么是聚 ...

  9. aria2 cmd set chmod, and others..

    import 'package:flutter/material.dart'; import 'dart:io'; import 'dart:async'; import 'package:rxdar ...

  10. 深入理解JVM-java内存区域与内存溢出异常

    1.内存模型概述 2.运行时数据区 2.1.程序计数器 理解: 1.什么是程序计数器 2.线程私有还是共享 引入难点: 理解什么是 native方法 简单地讲,一个Native Method就是一个j ...