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. 使用JavaMail发送邮件-no object DCH for MIME type multipart/mixed报错解决

    最近需要实现一个使用Spring schedule按一定时间间隔自动触发条件发送邮件的功能,在开发的过程中,是按照先测试能发出text/html文本邮件,然后测试添加附件发送邮件,我碰到的问题是,文本 ...

  2. Physical (Raw) Versus Logical Backups

    [Physical (Raw) Versus Logical Backups] Physical backups consist of raw copies of the directories an ...

  3. MySQL Keynote

    [MySQL Keynote] 1.Keywords may be entered in any lettercase. The following queries are equivalent: 2 ...

  4. linux 后台运行命令

    command & 关闭终端,程序会终止 nohup command & 关闭终端,程序不会终止

  5. nginx配置 解决ajax请求跨域问题

    文章来源:http://to-u.xyz/2016/06/30/nginx-cors/ 背景描述 最近在研究RESTful API接口设计,使用的是Nginx,要实现本地http://127.0.0. ...

  6. pta7-20 畅通工程之局部最小花费问题(Kruskal算法)

    题目链接:https://pintia.cn/problem-sets/15/problems/897 题意:给出n个城镇,然后给出n×(n-1)/2条边,即每两个城镇之间的边,包含起始点,终点,修建 ...

  7. CentOS 下搭建Tomcat

    1.下载tomcat软件包 wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.ta ...

  8. Camera插件推荐,解锁电影大师级视角控制

    相机在游戏中的重要性是不言而喻的,尤其是一些MMORPG或FPS等类型的游戏,相机不仅需要跟随游戏主角进行移动,可能还要随时准备切换焦点,这就要求开发者将游戏相机管理得井井有条,能顺应游戏中可能瞬息发 ...

  9. TZOJ 1937 Hie with the Pie(floyd+状压dp)

    描述 The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfo ...

  10. TZOJ 2648 小希的迷宫(并查集)

    描述 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道 ...