【LeetCode】325. Maximum Size Subarray Sum Equals k 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
目录
题目地址: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++)的更多相关文章
- [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 ...
- LeetCode 325. Maximum Size Subarray Sum Equals k
原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...
- 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题是存储的当前累加和的 ...
- 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 ...
- 325. Maximum Size Subarray Sum Equals k
最后更新 二刷 木有头绪啊.. 看答案明白了. 用的是two sum的思路. 比如最终找到一个区间,[i,j]满足sum = k,这个去见可以看做是 [0,j]的sum 减去 [0,i]的Sum. 维 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- 44-Count and Say
Count and Say My Submissions QuestionEditorial Solution Total Accepted: 79863 Total Submissions: 275 ...
- ubuntu安装配置ssh-connect to host localhost port 22: Connection refused
在安装ssh,经常出现 ssh: connect to host localhost port 22: Connection refused 从以下几点去检查: 1.是否安装ssh-server: 打 ...
- Freeswitch 安装爬坑记录1
2 Freeswitch的安装 2.1 准备工作 服务器安装CentOS 因为是内部环境,可以关闭一些防火墙设置,保证不会因为网络限制而不能连接 关闭防火墙 查看防火墙 systemctl statu ...
- 学习java的第十天
一.今日收获 1.java完全学习手册第二章2.9程序流程控制中的选择结构与顺序结构的例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.例题的问题不大,需要注意大小写,新的语句记忆不牢 2.哔哩哔 ...
- webpack打包报错 ERROR in ./js/ww.js from UglifyJs Unexpected token keyword «function», expected punc «,» [src/page/ww/view/xx/xx.vue:119,0][./js/ww.js:55218,17]
找了好多解决办法 你可以试着将babel-loader的exclude注释掉,然后看能否打包成功.如果可以,那就是这个问题.你只需要在vue.config.js中配置transpileDependen ...
- linux shell中的条件判断语句
http://bbs.chinaunix.net/thread-396805-1-1.html shell 判断语句 流程控制 "if" 表达式 如果条件为真则执行then后面的部 ...
- 【编程思想】【设计模式】【创建模式creational】原形模式Prototype
Python版 https://github.com/faif/python-patterns/blob/master/creational/prototype.py #!/usr/bin/env p ...
- 【Python】【Module】time
#_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了 ...
- 解决git push报错error: failed to push some refs to 的问题
这个问题发生的背景一般是: 想把自己本地的某个项目关联到远程仓库并推送上去,接着他会做如下操作: 本地项目->远程创建仓库->本地关联远程->推送最新代码 最后一个步骤发生问题: 那 ...
- Maven配置大全
maven项目打jar包(带依赖) <build> <plugins> <plugin> <artifactId>maven-assembly-plug ...