[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 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?
分析:
最大和子序列,最长连续(或不连续)的递增(或递减)子序列,时间复杂度都是O(n),又来一个和为k的最长连续子序列,要求时间复杂度也是O(n)。首先想到朴素的方法,将所有sum[i][j] = s[j] - s[i]都计算出来,找个长度最长且和为k的子序列,复杂度为O(n^2);怎么将复杂度缩减到n呢,考虑到如果对于每个j不通过遍历的方式找到和为k的i,而是直接确定和为k的i,那么复杂度即为O(n);直接确定的方式也比较容易想到,就是用hash map。
代码:
int maxSub(vector<int> &nums, int k) {
int sum = , maxLength = ;
unordered_map<int, int> hash;
hash[] = ;
for(int i = ; i < nums.size(); i++) {
sum += nums[i];
if(hash.find(sum - k) != hash.end())
maxLength = max(maxLength, i - hash[sum - k]);
//保证结果是最长子序列,所以让靠前的sum留下来
else if(hash.find(sum) == hash.end())
hash[sum] = i;
}
return maxLength;
}
[Locked] Maximum Size Subarray Sum Equals k的更多相关文章
- 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 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- [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] 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 ...
- Maximum Size Subarray Sum Equals k LT325
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 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 prefix Sum 日期 题目地址:https:// ...
- LeetCode Maximum Size Subarray Sum Equals k
原题链接在这里:https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/ 题目: Given an array nums an ...
随机推荐
- SQL觸發器聯級刪除
Create TRIGGER [dbo].[trigInstructionsDelete] ON dbo.Instructions instead OF DELETE AS BEGIN DECLARE ...
- CouchBase 遇到问题笔记(一)
刚开始看CouchBase,按照官网给出的示例,边敲边理解,遇到了一个很奇怪的问题,如下代码: IView<IViewRow> view = client.GetView("be ...
- 段落排版--缩进(text-indent)
中文文字中的段前习惯空两个文字的空白,这个特殊的样式可以用下面代码来实现: p{text-indent:2em;} <p>1922年的春天,一个想要成名名叫尼克卡拉威(托比?马奎尔Tobe ...
- 使用EasyUI导入的js顺序
使用Jquery Easy UI要导入的js顺序<1>.引用Jquery的Js文件<script src="jquery-easyui-1.3.4/jquery-1.8.0 ...
- [转]MySQL数据库备份和还原的常用命令小结
MySQL数据库备份和还原的常用命令小结,学习mysql的朋友可以参考下: 备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword datab ...
- ScheduleThreadPoolExecutor源码分析(二)
DelayedWorkQueue: DelayedWorkQueue实现了BlockingQueue接口,因此其可以作为线程池的任务队列.BlockingQueue的主要属性有以下几个: privat ...
- C#之移动无标题栏窗体功能的实现!...
为实现移动无标题栏窗体的功能,我从网上寻找.整理了以下资料,以备不时之需: 该方法适用于有标题栏和无标题栏窗体,适用于窗体内控件,当然 Form 也不例外, 只须添加 MouseDown.MouseM ...
- 深入解析.NET框架
一.AOP框架 Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase 独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种 ...
- input file文件上传样式
<style> .file-group { position: relative; width: 200px; height: 80px; ...
- 浏览器JS报错Uncaught RangeError: Maximum call stack size exceeded?
JavaScript错误:Uncaught RangeError: Maximum call stack size exceeded 堆栈溢出 原因:有小类到大类的递归查询导致溢出 解决方法思想: A ...