Continuous Subarray Sum LT523
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:
- The length of the array won't exceed 10,000.
- 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的更多相关文章
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- LintCode 402: Continuous Subarray Sum
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标 ...
- Continuous Subarray Sum II(LintCode)
Continuous Subarray Sum II Given an circular integer array (the next element of the last element i ...
- 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题是存储的当前累加和的 ...
- [LintCode] Continuous Subarray Sum 连续子数组之和
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- Continuous Subarray Sum
Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your cod ...
- [LeetCode] Continuous Subarray Sum 连续的子数组之和
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- [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 ...
- [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 ...
随机推荐
- iftop网络流量查看工具
1.下载iftop源码包 mkdir /usr/local/src/iftop cd /usr/local/src/iftop wget http://www.ex-parrot.com/~pdw/i ...
- Tcp/Ip 三次握手与四次挥手
1. TCP/IP模型 我们一般知道OSI的网络参考模型是分为7层:“应表会传网数物”——应用层,表示层,会话层,传输层,网络层,数据链路层,物理层.而实际的Linux网络层协议是参照了OSI标准,但 ...
- 算法之二分查找PK线性查找
列表查找(线性查找) 本质就是列表的index() 顺序查找 也叫线性查找,从列表第一个元素开始,顺序进行搜索,知道找到元素或搜索到列表最后一个元素为止. 以下是示例代码: def line_sear ...
- ss源码学习--工作流程
ss的local端和server端的工作流程相似,因此复用了TCPRelay类和TCPRelayHandler类. 两端均是使用TCPRelay类监听连接,并使用TCPRelayHandler类处理请 ...
- Django model 中的 class Meta 详解
Django model 中的 class Meta 详解 通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(mode ...
- dedecms迁站
1 后台>系统>备份数据库 2 下载“所有(强调一下是所有:包括整站程序与备份的数据)”原站的数据,整个站点 3 将下载下来的所有数据上传到新空间 4 删除install目录下的i ...
- CUDA 编程的基本模式
reproduced from: http://www.cnblogs.com/muchen/p/6306747.html 前言 本文将介绍 CUDA 编程的基本模式,所有 CUDA 程序都基于此模式 ...
- Python compile() 函数
Python compile() 函数 Python 内置函数 描述 compile() 函数将一个字符串编译为字节代码. 语法 以下是 compile() 方法的语法: compile(sourc ...
- Jasperreport5.6.9-----1
Jasperreport5.6.0生成PDF 最近项目中需要生成报表,先是看了下itext,觉得还可以,但是如果数据字段多的话,不太灵活.所以后来看了下ireport,觉得还可以,下面简单说一下它们: ...
- cookie与webStorage区别