[LintCode笔记了解一下]41.Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sum.
首先
當題目涉及到求最大最小值時,最初的比較數字就應當設置爲INT_MAX或INT_MIN,更爲安全。 <limits.h>中有INT_MAX和INT_MIN的宏定義可直接使用。 或者自行定義宏 #define INT_MAX 0x7fffffff #define INT_MIN 0x80000000 INT_MAX = INT_MIN = -
然后
思路一:
进行两次循环,遍历所有可能的情况,找到最大的子数组,时间复杂度为O(n^2);
思路二:
对于任意一个子数组和,如果大于0,那么它再添加一个数,他的贡献是正值,如果子数组和小于0,再添加一个数,它的贡献则为负值,这时候不如将当前子数组舍掉,新数成为一个新数组。(动态规划Dynamic Programming)
思路一
public int maxSubArray(int[] nums) {
// write your code
if (nums.length == ) return nums[];
int max = nums[];
for (int i = ; i < nums.length; i++){
int temp = nums[i];
for (int j = i+; j < nums.length; j++){
temp += nums[j];
if (temp > max){
max = temp;
}
}
}
return max;
}
思路二
public int maxSubArray(int[] nums) {
// write your code
int max = Integer.MIN_VALUE;
int sum = Integer.MIN_VALUE;
for (int i = ; i < nums.length; i++){
sum = sum< ? nums[i] : nums[i]+sum;
if (sum>max){
max = sum;
}
}
return max;
}
[LintCode笔记了解一下]41.Maximum Subarray的更多相关文章
- [LintCode笔记了解一下]44.Minimum Subarray
这道题和max subarray很类似,我用local 和 global 的dp方式阔以解决这道 那么我们来看动态规划的四个要素分别是什么? State: localmin[i] 表示以当前第i个数最 ...
- 41. Maximum Subarray
Description Given an array of integers, find a contiguous subarray which has the largest sum. The su ...
- [LintCode] Maximum Subarray 最大子数组
Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
- (转)Maximum subarray problem--Kadane’s Algorithm
转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...
- 【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 ...
随机推荐
- maven release plugin插件
1.打包版本区别 SNAPSHOT 快照版本(开发阶段,不稳定,容易出现bug)RELEASE 正式版本(外部依赖使用阶段,稳定,很少出现bug)Tag :标记每次代码提交的版本(比较稳定,类似分支) ...
- react组件生命
组件的生命周期主要由三个部分组成: Mounting:组件正在被插入DOM中 Updating:如果DOM需要更新,组件正在被重新渲染 Unmounting:组件从DOM中移除 React提供了方法, ...
- 简单获取各大视频网站的flash地址
最近做网站的时候遇到一个需求:给定一个视频地址,获取它的swf地址.例如,给一个优酷的视频地址:http://v.youku.com /v_show/id_XNDg4MzY5ODU2.html,想获取 ...
- python 监控windows磁盘空间和备份大小
#!/usr/bin/env python # Version = 3.5.2 # __auth__ = '无名小妖' import os import time import sendmail im ...
- Weak References
http://docwiki.embarcadero.com/RADStudio/Seattle/en/Automatic_Reference_Counting_in_Delphi_Mobile_Co ...
- Android apk couldn't install
an existing package with the same name and signature is already installed
- 使用EXCEL绘制三维地图(超简单的五分钟绘制地图方法,妈妈再也不用担心我不会画地图啦~)
博主为从区域规划转行地图学的小学渣一枚,最近处理数据希望对结果进行三维可视化,意外发现从小用到大的EXCEL可以绘制地图且功能非常强大,在这里做一下简单介绍,希望可以给看官提供些许帮助.那下面就开始吧 ...
- 设置Eclipse中的tab键为4个空格
在工具栏上找到window,点击 window->preference 然后在打开的界面中选择 General->Editors->Text Editors, 再选中右侧的 inse ...
- Struts2 的核心、执行原理
转自: http://www.cnblogs.com/xiadongqing/p/5240615.html 在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们带来什么样 ...
- QT与HALCON(入门)
#include "qtdesign.h" #include <QtGui/QApplication> #include "halconcpp.h" ...