Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn't one, return 0 instead.

Note:
The sum of the entire nums array is guaranteed to fit within the 32-bit signed integer range.

Example 1:

Given nums = [1, -1, 5, -2, 3], k = 3,
return 4. (because the subarray [1, -1, 5, -2] sums to 3 and is the longest)

Example 2:

Given nums = [-2, -1, 2, 1], k = 1,
return 2. (because the subarray [-1, 2] sums to 1 and is the longest)

Follow Up:
Can you do it in O(n) time?

Idea 1. HashMap to store (prefixSum, the first index prefixSum ends) + prefix subarray sum.

Time complexity: O(n)

Space complexity: O(n)

 public class Solution {
public int maxSubArrayLen(int[] nums, int k) {
int maxLen = 0;
Map<Integer, Integer> sumIndex = new HashMap<>();
sumIndex.put(0, -1); int sum = 0;
for(int i = 0; i < nums.length; ++i) {
sum += nums[i];
Integer left = sumIndex.get(sum - k);
if(left != null) {
maxLen = Math.max(maxLen, i - left);
}
sumIndex.putIfAbsent(sum, i);
}
return maxLen;
}
}

Maximum Size Subarray Sum Equals k LT325的更多相关文章

  1. Subarray Sum & Maximum Size Subarray Sum Equals K

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

  2. Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K

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

  3. 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题是存储的当前累加和的 ...

  4. [LeetCode] 325. 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 ...

  5. [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 ...

  6. Maximum Size Subarray Sum Equals k -- LeetCode

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

  7. [Locked] Maximum Size Subarray Sum Equals k

    Example 1: Given nums = [1, -1, 5, -2, 3], k = 3,return 4. (because the subarray [1, -1, 5, -2] sums ...

  8. 【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...

  9. LeetCode Maximum Size Subarray Sum Equals k

    原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...

随机推荐

  1. IN_ITEMBOMROUTING中的数据被过滤 到IN_ITEMBOMROUTING_TEMP

    ' ; 解决方法: 检查如下数据是否存在就可以知道为什么被过滤 掉 ' ) ' )

  2. CSSの変数を使う

    この文章はhttps://developer.mozilla.org/ja/docs/Web/CSS/Using_CSS_variablesを参考します. これは実験段階の機能です.この機能は複数のブ ...

  3. (django1.10)访问url报错Forbidden (CSRF cookie not set.): xxx

    问题:页面访问时报错 Forbidden (CSRF cookie not set.): xxx     解决方法: 修改settings.py文件,注释掉 django.middleware.csr ...

  4. UML中的关联,泛化,依赖,聚集,组合(转)

    转自:http://blog.sina.com.cn/s/blog_5f8b45f20100dzjo.html 关联(association): 这是一种很常见的关系,这种关系在我们的生活中到处可见, ...

  5. 忘记root密码,怎么办

    当前账户拥有sudo权限,可以通过sudo passwd root来重置root密码.

  6. apache jmeter 压力测试

    前面讲了linux下的压力测试,今天来个windows下的,用jmeter为例 我用了两个apache-jmeter-3.1和apache-jmeter-4.0分别进行了测试, 前者高并发电脑卡死时间 ...

  7. echarts中国地图散点涟漪效果

    echarts中国地图例子:http://gallery.echartsjs.com/editor.html?c=effectScatter-map 代码: var data = [{ name: ' ...

  8. LWP::UserAgent的用法

    LWP::UserAgent是一个模拟用户浏览器的类,在使用的时候需要遵守以下几步: 1.引入模块 2.创建一个LWP::UserAgent的对象 3.设置这个对象的相关参数 4.创建HTTP::Re ...

  9. linux命令学习之:ifup/ifdown

    ifup命令网络配置 ifup命令用于激活指定的网络接口.ifdown命令用于禁用指定的网络接口. 实时地手动修改一些网络接口参数,可以利用ifconfig来实现,如果是要直接以配置文件,亦即是在 / ...

  10. tomcat配置的环境变量catalina.home和catalina.base 区别

    本篇文章原创地址为:http://blog.csdn.net/you23hai45/article/details/27726147 这两个属性仅在你需要安装多个Tomcat实例而不想安装多个软件备份 ...