Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums up to the multiple of k, that is, sums up to n*k where n is also an integer.

Example 1:

Input: [23, 2, 4, 6, 7],  k=6
Output: True
Explanation: Because [2, 4] is a continuous subarray of size 2 and sums up to 6.

Example 2:

Input: [23, 2, 6, 4, 7],  k=6
Output: True
Explanation: Because [23, 2, 6, 4, 7] is an continuous subarray of size 5 and sums up to 42.

Note:

  1. The length of the array won't exceed 10,000.
  2. You may assume the sum of all the numbers is in the range of a signed 32-bit integer.

Idea 1: Brute force Compute the sum of each subarray for a given pair of interger 0 <= i <= j-1 <= nums.length-1. The size of the subarray is at least 2,  j - i + 1 >= 2, that is j - i >= 1. Check sum[i..j]%k == 0 if k != 0

Note that k could be 0, sum[i..j] = 0

Time complexity: O(n2)

Space complexity: O(1)

 class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
for(int i = 0; i < nums.length; ++i) {
int sum = 0;
for(int j = i; j < nums.length; ++j) {
sum += nums[j];
if( k == 0 ) {
if(sum == 0 && (j - i >= 1)){
return true;
}
}
else if(sum%k == 0 && j-i >= 1) {
return true;
}
}
} return false;
}
}

Idea 2:  If the remainder of the cumulative sum cumuSum[0...j] ( = k * x + mod1) is equal to that of cumuSum[0...i] ( = k * y + mod2), it means the subarray sum between i and j is a multiple of k, since cumuSum[j] - cumuSum[i] = (x - y) * k.

Note that k could be 0, this case needs to deal differently, cumuSum[j] - cumuSum[i] == 0

2.a brute force

Time complexity: O(n2)

Space complexity: O(n)

 class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
int sz = nums.length;
int[] cumuSum = new int[sz+1]; for(int i = 1; i <= sz; ++i) {
cumuSum[i] = cumuSum[i-1] + nums[i-1];
} for(int i = 0; i <= sz; ++i) {
for(int j = i+2; j <= sz; ++j) {
if(k == 0) {
if(cumuSum[j] - cumuSum[i] == 0) {
return true;
}
}
else if((cumuSum[j] - cumuSum[i]) % k == 0) {
return true;
}
}
} return false;
}
}

2.b Take the hashMap solution used in Subarray Sum Equals K LT560, we can store (cumuSum, index of the cumuSum) as map entry, for each new element in the array, firstly adding it to get the cumuSum, if k!= 0, cumuSum = cumuSum%mod, then return true if hashMap has the same cumuSum and i - (index+1) + 1 >= 2, since the subarray[index+1..i], only add cumuSum to the map when the cumuSum does not exist(which handles the case nums = [0, 0], k = 0; nums= [0, 5, 5], k = 5), othwise it will overwrite the previous position.

Note to deal the case when cumuSum[0..i] = n * k, after moduler operation, the mod becomes 0, so we have to put a pair with key = 0 to the map, the leftmost index for a subarray with size to be at least two is i = 1, i - index >= 2, hence the value = -1.

Time complexity: O(n)

Space complexity: O(n), not O(k) take nums = {1,1,1,1,1} and k = 0, generally it's O(k) is k != 0

 class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> sumIndex = new HashMap<>(); sumIndex.put(0, -1);
int sum = 0;
for(int i = 0; i < nums.length; ++i) {
sum += nums[i];
if(k != 0) {
sum = sum%k;
} Integer index = sumIndex.get(sum);
if(index != null) {
if(i - index >= 2) {
return true;
}
}
else sumIndex.put(sum, i);
} return false;
}
}

Idea 2.c: Set. Since the size of the subarray is required to be at least 2, we can't store the cumuSum immediately, need to make the gap and store the previous sum ending at i-1 at the end of index i, hence at the start of next index (i+1), the sum stored in the set is at least 2 steps away.

Time complexity: O(n)

Space complexity: O(n)

 class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Set<Integer> sumMap = new HashSet<>(); int prev = 0;
int sum = 0;
for(int i = 0; i < nums.length; ++i) {
sum += nums[i];
if(k != 0) {
sum = sum%k;
} if(sumMap.contains(sum)) {
return true;
} sumMap.add(prev);
prev = sum;
} return false;
}
}

Continuous Subarray Sum LT523的更多相关文章

  1. [LintCode] Continuous Subarray Sum II

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

  2. LintCode 402: Continuous Subarray Sum

    LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...

  3. Continuous Subarray Sum II(LintCode)

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

  4. leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)

    整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...

  5. [LintCode] Continuous Subarray Sum 连续子数组之和

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

  6. Continuous Subarray Sum

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

  7. [LeetCode] Continuous Subarray Sum 连续的子数组之和

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  8. [Swift]LeetCode523. 连续的子数组和 | Continuous Subarray Sum

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

  9. [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)

    Given a list of non-negative numbers and a target integer k, write a function to check if the array ...

随机推荐

  1. sql数据库之多库查询

    连接到数据库服务器gwsps07上,打开查询分析器,如何获取gwrenshi数据库中的数据? 查询语句如下: select * from GWRENSHI.CGC.dbo.PERempms(serve ...

  2. 解题(JuZhengCalculate-矩阵乘法计算量)

    题目描述 矩阵乘法的运算量与矩阵乘法的顺序强相关. 例如: A是一个50×10的矩阵,B是10×20的矩阵,C是20×5的矩阵 计算A*B*C有两种顺序:((AB)C)或者(A(BC)),前者需要计算 ...

  3. JMeter学习(四)参数化(转载)

    转载自 http://www.cnblogs.com/yangxia-test JMeter也有像LR中的参数化,本篇就来介绍下JMeter的参数化如何去实现. 参数化:录制脚本中有登录操作,需要输入 ...

  4. day25 面向对象之多态和鸭子类型

    1.封装方法 如何封装:给方法名称前面加上双下划线 # ATM 的取款功能 # 1.插入银行卡 2.输入密码 3.选择取款金额 4.取款 class ATM: def __insert_card(se ...

  5. python对ftp进行操作

    背景:需要对ftp进行操作,涉及上传和下载操作   from ftplib import FTP ftp = FTP(host=“ftp的hostname",user=‘登录用户名', pa ...

  6. python使用函数作为参数

    在实际使用中,我们有时希望将函数作为参数传递给另一个方法使用. 比如装饰器实际就是函数调用函数   举个例子,我想传递在调用方法之前打印一下时间:   使用函数当做入参 那就可以把方法名A当做入参传递 ...

  7. centos7.4上安装python3环境的坑

    前言:为了将爬虫项目布置到服务器上,才有了今天这一下午的坑,必须记录 不要动现有的python2环境!不要动现有的python2环境!不要动现有的python2环境! 解压 tar -xvf Pyth ...

  8. SpringBoot配置logback

    1.在SpringBoot中已经集成了logback.在pom.xml中加入以下spring-boot-starter依赖,使用默认版本即可: <dependency> <group ...

  9. cloudera cdh5.13.0 vmware 快速安装

    1. 从官网上载VMWARE VM快速安装包 https://www.cloudera.com/downloads/quickstart_vms/5-12.html 2. 下载后的安装包,解压之后得到 ...

  10. Class语法糖

    TypeScript源码 class A { hello() { } } class B extends A{ welcome() { } } TypeScript编译 var __extends = ...