题目地址:https://leetcode-cn.com/problems/maximum-size-subarray-sum-equals-k/

题目描述

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:

Input: nums = [1, -1, 5, -2, 3], k = 3
Output: 4
Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.

Example 2:

Input: nums = [-2, -1, 2, 1], k = 1
Output: 2
Explanation: The subarray [-1, 2] sums to 1 and is the longest.

Follow Up:

  • Can you do it in O(n) time?

题目大意

给定一个长度为 n 的整数数组和一个目标值 target,寻找能够使条件 nums[i] + nums[j] + nums[k] < target 成立的三元组 i, j, k 个数(0 <= i < j < k < n)

解题方法

prefix Sum

很常见的做法,求数组每个位置的prefix sum(前缀和),并把该sum和其对应的位置i放到字典里保存,由于我们需要找到最长的,因此如果有重复的prefix sum不可以覆盖之前的值。

知道了目标的target是k,每个位置的prefix sum,我们想得到prefix[i] - prefix[j] = k,所以在i位置我们可以向前寻找到prefix[j] = prefix[i] - k,这个值可以从字典中直接读取出来。这个就是以i为结尾的最长区间。

我下面的做法为了方便,preSum在最起始位置增加了一个0,其索引位置是-1。

另外这个题中,需要注意的是区间的长度是不是要+1的问题,答案是不用+1,因为知道的prefix[j]是区间起始位置的前一个位置,即真正的和是k的区间是[j + 1, i]双闭区间,其区间长度是i - j

C++代码如下:

class Solution {
public:
int maxSubArrayLen(vector<int>& nums, int k) {
const int N = nums.size();
vector<int> preSum(N + 1, 0);
unordered_map<int, int> m;
m[0] = -1;
int res = 0;
for (int i = 0; i < N; ++i) {
preSum[i + 1] += preSum[i] + nums[i];
if (!m.count(preSum[i + 1])) {
m[preSum[i + 1]] = i;
}
int cur = preSum[i + 1] - k;
if (m.count(cur)) {
res = max(res, i - m[cur]);
}
}
return res;
}
};

日期

2019 年 9 月 22 日 —— 熬夜废掉半条命

【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)的更多相关文章

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

  2. LeetCode 325. Maximum Size Subarray Sum Equals k

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

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

  5. 325. Maximum Size Subarray Sum Equals k

    最后更新 二刷 木有头绪啊.. 看答案明白了. 用的是two sum的思路. 比如最终找到一个区间,[i,j]满足sum = k,这个去见可以看做是 [0,j]的sum 减去 [0,i]的Sum. 维 ...

  6. 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 ...

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

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

  9. 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 ...

随机推荐

  1. pcm-pcie 解析

    简介 pcm 全称为 Performance Counter Monitor,该项目是针对 intel 平台处理器的资源利用率进行监控的工具.在现代 Intel 处理器已经提供了监视处理器内部性能事件 ...

  2. C#多个标题头合并

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { switch (e.Row.RowType) ...

  3. android studio 编译 Android dependency has different version

    找了一圈,终于在大佬的博客中找到了解决方法. 附链接:https://blog.csdn.net/u010725171/article/details/81232183 Android depende ...

  4. A Child's History of England.15

    And indeed it did. For, the great army landing from the great fleet, near Exeter, went forward, layi ...

  5. 容器之分类与各种测试(四)——multimap

    multiset和multimap的具体区别在于,前者的key值就是自己存储的value,后者的key与value是分开的不相关的. 例程 #include<stdexcept> #inc ...

  6. 【Linux】【Basis】用户、组和权限管理

    1. 概念: 1.1. 每个使用者都有,用户标识UID和密码:并且有身份(Authentication),授权(Authorization),审计(Audition):组(用户组,用户容器) 1.2. ...

  7. FastDFS的理解和分析

    FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相 ...

  8. 基于spring sringmvc mybatis 做的导入导出

    导入 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://ww ...

  9. spring下春注解的声明式事务控制

    package com.hope.test;import com.hope.domain.Account;import com.hope.service.IAccountService;import ...

  10. 【力扣】146. LRU缓存机制

    运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) - 如果关键字 (key) ...