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的更多相关文章

  1. [LintCode笔记了解一下]44.Minimum Subarray

    这道题和max subarray很类似,我用local 和 global 的dp方式阔以解决这道 那么我们来看动态规划的四个要素分别是什么? State: localmin[i] 表示以当前第i个数最 ...

  2. 41. Maximum Subarray

    Description Given an array of integers, find a contiguous subarray which has the largest sum. The su ...

  3. [LintCode] Maximum Subarray 最大子数组

    Given an array of integers, find a contiguous subarray which has the largest sum. Notice The subarra ...

  4. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

  5. (转)Maximum subarray problem--Kadane’s Algorithm

    转自:http://kartikkukreja.wordpress.com/2013/06/17/kadanes-algorithm/ 本来打算自己写的,后来看到上述链接的博客已经说得很清楚了,就不重 ...

  6. 【leetcode】Maximum Subarray (53)

    1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one nu ...

  7. 算法:寻找maximum subarray

    <算法导论>一书中演示分治算法的第二个例子,第一个例子是递归排序,较为简单.寻找maximum subarray稍微复杂点. 题目是这样的:给定序列x = [1, -4, 4, 4, 5, ...

  8. LEETCODE —— Maximum Subarray [一维DP]

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

  9. 【leetcode】Maximum Subarray

    Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which ...

随机推荐

  1. PKU1988磁铁

    Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 16258   Accepted: 5579 Ca ...

  2. Tkinter Cursors

      Tkinter Cursors:   Python的Tkinter的支持很多不同的鼠标光标的数字.确切的图形可能会有所不同,根据您的操作系统. 这里是大量有趣的的名单: "arrow&q ...

  3. [Z] 关于Python Tornado的一些资料

    一个简单的样例: http://osedu.net/article/python/2014-03-18/501.html ioloop的官方doc: http://www.tornadoweb.org ...

  4. 在springBoot在控制台打印sql语句

    在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增: logging.level.com.dy.springboot.server.mapper= ...

  5. (转) iphone开发资源汇总

    如何用Facebook graphic api上传视频: http://developers.facebook.com/blog/post/532/ Keychain保存数据封装: https://g ...

  6. Python列表的生成

    要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],我们可以用range(1, 11): >>> range(1, 11) [1, 2, 3, 4, 5, ...

  7. Java的SSH网站

    1.框架 strusts2 + Hibernate + spring 2.图片 图1-1 网站结构 图1-2 java代码结构 3.源代码 3.1 UserAction.java package co ...

  8. UV-Sprite动画

    [UV-Sprite动画] 下文以单行Sprite纹理作为动画贴图.首先需要输入纹理宽度.Sprint数量.速度: 计算每个Sprite的像素宽与UV宽: 根据_Time,计算当前显示第几个Sprit ...

  9. schedule与scheduleAtFixedRate之Timer源码分析

    执行Timer任务调度方法有如下几种: 这些方法最后调用的都是这个方法: private void sched(TimerTask task, long time, long period)   这个 ...

  10. 使用CreateJS绘制数字盒子