LintCode: Maximum Subarray
1. 暴力枚举
2. “聪明”枚举
3. 分治法
分:两个基本等长的子数组,分别求解T(n/2)
合:跨中心点的最大子数组合(枚举)O(n)
时间复杂度:O(n*logn)
class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
// write your code here
int size = nums.size();
if (size == ) {
return nums[];
}
int *data = nums.data();
return helper(data, size);
}
int helper(int *data, int n) {
if ( n == ) {
return data[];
}
int mid = n >> ;
int ans = max(helper(data, mid), helper(data + mid, n - mid));
int now = data[mid - ], may = now;
for (int i = mid - ; i >= ; i--) {
may = max(may, now += data[i]);
}
now = may;
for (int i = mid; i < n; i++) {
may = max(may, now += data[i]);
}
return max(ans, may);
}
};
4. dp(不枚举子数组,枚举方案)
dp[i]表示以a[i]结尾的最大子数组的和
dp[i] = max(dp[i-1]+a[i], a[i])
包含a[i-1]:dp[i-1]+a[i]
不包含a[i-1]:a[i]
初值:dp[0] = a[0]
答案:最大的dp[0...n-1]
时间:O(n)
空间:O(n)
空间优化:dp[i]要存吗?
endHere = max(endHere+a[i], a[i])
answer = max(endHere, answer)
优化后的空间:O(1)
class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
// write your code here
int size = nums.size();
if (size == ) {
return nums[];
}
vector<int> dp(size);
dp[] = nums[];
int ans = dp[];
for (int i=; i<size; i++) {
dp[i] = max(dp[i - ] + nums[i], nums[i]);
ans = max(ans, dp[i]);
}
return ans;
}
};
空间优化
class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
// write your code here
int size = nums.size();
if (size == ) {
return nums[];
}
int endHere = nums[];
int ans = nums[];
for (int i=; i<size; i++) {
endHere = max(endHere + nums[i], nums[i]);
ans = max(ans, endHere);
}
return ans;
}
};
5. 另外一种线性枚举
定义:sum[i] = a[0] + a[1] + a[2] + ... + a[i] i>=0
sum[-1] = 0
则对0<=i<=j:
a[i] + a[i+1] + ... + a[j] = sum[j] - sum[i-1]
我们就是要求这样一个最大值:
对j我们可以求得当前的sum[j],取的i-1一定是之前最小的sum值,用一个变量记录sum的最小值
时间:O(n)
空间:O(1)
class Solution {
public:
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
int maxSubArray(vector<int> nums) {
// write your code here
int size = nums.size();
if (size == ) {
return nums[];
}
int sum = nums[];
int minSum = min(, sum);
int ans = nums[];
for (int i = ; i < size; ++i) {
sum += nums[i];
ans = max(ans, sum - minSum);
minSum = min(minSum, sum);
}
return ans;
}
};
LintCode: Maximum Subarray的更多相关文章
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- Lintcode: Maximum Subarray III
Given an array of integers and a number k, find k non-overlapping subarrays which have the largest s ...
- Lintcode: Maximum Subarray Difference
Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...
- Lintcode: Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum. The numbe ...
- 【leetcode】Maximum Subarray (53)
1. Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...
- 算法:寻找maximum subarray
<算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...
- LEETCODE —— Maximum Subarray [一维DP]
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- 【leetcode】Maximum Subarray
Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...
- maximum subarray problem
In computer science, the maximum subarray problem is the task of finding the contiguous subarray wit ...
随机推荐
- 报错:Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
在保存数据的时候报这个错误,知道是验证错误,但到底是哪个属性验证错误呢? →打断点,运行,观察程序出错的地方→在出错的部分添加try...catch语句块→添加监视,输入((System.Data.E ...
- Linux下Tomcat的启动、关闭
在Linux系统下,启动和关闭Tomcat使用命令操作. 进入Tomcat下的bin目录 1 cd /java/tomcat/bin 启动Tomcat命令 1 ./startup.sh 停止Tomca ...
- BI 可视化
1. Blackbird: Open Source JavaScript Logging Utility Blackbird 是一款非常酷的 JavaScript 调试工具,带有一个漂亮的界面显示和过 ...
- SharePoint 获取详细Log信息
在SharePoint的运维当中,我们可能经常会遇到排错,但是即使找到日志,也不是特别的详细,我们还是需要各种无厘头的猜测. 其实,SharePoint是可以打开详细的日志的,尤其是面对一些服务产生的 ...
- mysql group by 报错异常解决
mysql报错及其解决方式 1.在使用group by 查询一张表的数据的时候:select date,time,max(delaytime) as delaytime,sum(delaynum) a ...
- Windows10更新后,远程桌面无法登录服务器 提示远程桌面协议 CredSSP 出现漏洞
Win10远程桌面 出现 身份验证错误,要求的函数不受支持,这可能是由于CredSSP加密Oracle修正 解决方法 打开注册表,手动建立 这个路径 HKEY_LOCAL_MACHINE\SOFTWA ...
- 总是容易忘记:enum、int、string之间的快速转换
public enum Color { Red=, Green= } (1)Enum转换为String Color.Read.ToString() Convert.ToString(Color.Gre ...
- 使用pm2管理node.js应用
中文文档:https://pm2.io/doc/zh/runtime/quick-start/ pm2是从nodejs衍生出来的服务器进程管理工具,可以做到开机就启动nodejs.当然了,有些运维同学 ...
- verilog语法实例学习(2)
Verilog中的信号类型 线网类型 线网类型表示一个或多个门或者其它类型的信号源驱动的硬件连线.如果没有驱动源,则线网的默认值为z.verilog中定义的线网类型有以下几种: wire,tr ...
- dcm4chee 修改默认(0002,0013) ImplementationVersionName
dcm4chee-2.17.3-psql\server\default\lib\dcm4che.jar ----org\dcm4che\Implementation.properties dcm4ch ...