题目:

给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。

样例

给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6

注意

子数组最少包含一个数

挑战

要求时间复杂度为O(n)

解题:

通过率37%,一定是暴力,时间复杂度O(N3)刷出来的成绩。。。

动态规划求解,维基百科

下面程序半个暴力吧,时间复杂度O(n2)

Java程序:

public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(ArrayList<Integer> nums) {
// write your code
int maxsum = Integer.MIN_VALUE;
for(int i = 0;i<nums.size();i++){
int sum = 0;
for(int j=i;j<nums.size();j++){
sum+=nums.get(j);
maxsum = Math.max(sum,maxsum);
}
}
return maxsum;
}
}

总耗时: 3227 ms

下面看到一个可以时间复杂度是O(N),但是只能对最大子数组的和大于0的时候才可以,,,但是最大子数组的和是负的,最大的那个负数就是答案了。

Java程序:

public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(ArrayList<Integer> nums) {
// write your code
int maxsum = Integer.MIN_VALUE;
int sum = 0;
for(int i = 0;i<nums.size();i++){
if ( sum < 0 ){
sum = 0;
}
sum += nums.get(i);
maxsum = Math.max(maxsum, sum); }
return maxsum;
}
}

总耗时: 1497 ms

Python程序:

class Solution:
"""
@param nums: A list of integers
@return: An integer denote the sum of maximum subarray
"""
def maxSubArray(self, nums):
# write your code here
if nums==None:
return 0
maxsum = -11111110
sum = 0
for i in range(len(nums)):
if sum<0:
sum=0
sum+=nums[i]
maxsum = max(sum,maxsum)
return maxsum

总耗时: 246 ms

动态规划求解:

Python程序:

class Solution:
"""
@param nums: A list of integers
@return: An integer denote the sum of maximum subarray
"""
def maxSubArray(self, nums):
# write your code here
max_ending_here = max_so_far = nums[0]
for x in nums[1:]:
max_ending_here = max(x, max_ending_here + x)
max_so_far = max(max_so_far , max_ending_here)
print x,max_ending_here,max_so_far
return max_so_far

上面的max_ending_here是包括当前位置时候的最大值,mas_so_far现阶段的最大值。这里理解的不是很透彻。。。

如:

nums -2 2 -3 4 -1 2 1 -5 3
max_ending_here -2 2 -1 4 3 5 6 1 4
max_so_far -2 2 2 4 4 5 6 6 6

Java程序:

public class Solution {
/**
* @param nums: A list of integers
* @return: A integer indicate the sum of max subarray
*/
public int maxSubArray(ArrayList<Integer> nums) {
// write your code
int max_ending_here = nums.get(0);
int max_so_far = nums.get(0);
for( int i =1 ;i<nums.size(); i++) {
max_ending_here = Math.max( nums.get(i) , nums.get(i) + max_ending_here );
max_so_far = Math.max( max_so_far, max_ending_here);
}
return max_so_far; }
}
总耗时: 1539 ms

lintcode :最大子数组的更多相关文章

  1. lintcode 最大子数组III

    题目描述 给定一个整数数组和一个整数 k,找出 k 个不重叠子数组使得它们的和最大.每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [-1 ...

  2. lintcode:最大子数组差

    题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1], ...

  3. lintcode:最大子数组II

    题目 最大子数组 II 给定一个整数数组,找出两个不重叠子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 样例 给出数组[1, 3, -1, 2, -1, 2], ...

  4. 最大子数组(LintCode)

    最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6 注意 子数 ...

  5. lincode.41 最大子数组

    最大子数组   描述 笔记 数据 评测 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? ...

  6. lintcode-42-最大子数组 II

    42-最大子数组 II 给定一个整数数组,找出两个 不重叠 子数组使得它们的和最大. 每个子数组的数字在数组中的位置应该是连续的. 返回最大的和. 注意事项 子数组最少包含一个数 样例 给出数组 [1 ...

  7. LintCode-41.最大子数组

    最大子数组 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2 ...

  8. lintcode-45-最大子数组差

    45-最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 注意事项 子数组最少包含一个数 样例 给出 ...

  9. C++:最大子数组差

    最大子数组差 内存限制:128 MiB        时间限制:1000 ms 题目描述: 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B) ...

随机推荐

  1. solr6安装部署

    难得写篇自己的原创文档了,哈哈哈,原谅我知识浅薄,积淀太少 一.涉及到的软件和环境jdk1.8.0_92,tomcat8,zookeeper3.4.8,solr6.1.0(solr6需要jdk8以上环 ...

  2. Silverlight 中DataGrid中全选与非全选问题

    问题:当点击全选时,全选所有的复选框,但是滚动屏幕时,却复选框就会取消选中 一.解决方法(将要展示的实体数据模型添加bool属性,在数据绑定时添加click时间,盘带选中的状态,就可以了) 1. xa ...

  3. jQuery 添加元素和删除元素

    jQuery - 添加元素 append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元 ...

  4. 从一个URL下载原始数据,基于byte字节,得到byte数组

    public static byte[] loadRawDataFromURL(String u) throws Exception { URL url = new URL(u); HttpURLCo ...

  5. Django之Model(一)--基础篇

    0.数据库配置 django默认支持sqlite,mysql, oracle,postgresql数据库.Django连接数据库默认编码使用UTF8,使用中文不需要特别设置. sqlite djang ...

  6. 图解 CSS: 理解样式表的逻辑(转载)

    原文:http://www.cnblogs.com/del/archive/2009/02/01/1382141.html 样式表可以是外部的.内联的或嵌入的; 链接外部样式文件一般是:<lin ...

  7. jquery-easyui中datagrid扩展,隐藏显示表头功能

    今天,后台中需要新增一个功能,用户可以自由选择显示的列,之后保存到本地localStroage中.所以扩展了easyui中datagrid的onHeaderContextMenu方法. 使用方法: _ ...

  8. (一)使用log4net生成日志文件

    1.引入log4net.dll 1.1 Nuget安装 或 http://logging.apache.org/log4net/下载log4net的源代码,编译后把log4net.dll引入项目. 2 ...

  9. C# 将cookiecontainer写到本地

    public static void WriteCookiesToDisk(string file, CookieContainer cookieJar) { using(Stream stream ...

  10. c# this.location和e.X的区别

    this.location是窗口当前位置 e.X是具体事件的相对坐标 size是窗口尺寸宽和高