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. 2.redis配置

    转自:http://www.runoob.com/redis/redis-tutorial.html Redis 的配置文件位于 Redis 安装目录下,文件名为 redis.conf. 你可以通过  ...

  2. chrom中安装elastic search head插件

    1. 访问https://chrome.google.com/webstore/detail/elasticsearch-head/ffmkiejjmecolpfloofpjologoblkegm/ ...

  3. 使用ExitProcess()结束本进程、TerminateProcess 结束进程

    进程只是提供了一段地址空间和内核对象,其运行时通过在其地址空间内的主线程来体现的.当主线程的进入点函数返回时,进程也就随之结束.这种进程的终止方式是进程的正常退出,进程中的所有线程资源都能够得到正确的 ...

  4. Bower 使用

    Bower:客户端库管理工具 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 目录 概述 常用操作 项目初始化 库的安装 库的搜索和查看 库的更新和卸载 列出所有库 ...

  5. Sqoop 1.99.6 安装和使用

        安装   1.安装准备工作:   下载的sqoop安装包 http://mirrors.hust.edu.cn/apache/sqoop/1.99.6/sqoop-1.99.6.tar.gz ...

  6. Specular Mask Texture

    [Specular Mask Texture] Specular Texture是一个Mask纹理,通过Mask纹理的texel可以控制每个像素的纹理. 在属性中添加Specular Mask Tex ...

  7. 【UVA11613 训练指南】生产销售规划 【费用流】

    题意: Acme公司生产一种X元素,给出该元素在未来M个月中每个月的单位售价.最大产量.最大销售量,以及最大储存时间(过期报废不过可以储存任意多的量).你的任务是计算出公司能够赚到的最大利润. 分析: ...

  8. 超出div宽度范围的文字进行省略号省略,在鼠标移上去以后显示完整的内容

    一.前言 当我们在固定的范围内显示内容时,我们是希望能够完整显示的,然而往往事与愿违,文本会超出我们给定的范围,这时候怎么办呢? 二.超出范围,对文本进行省略号隐藏 先上图 代码很简单 div{ wi ...

  9. git回滚到某个commit 上和 返回最新的版本git

    1. 代码回退 首先你要用git log 查看你要回到的那个本版, 然后用 git reset --hard HEAD^ 回退到上个版本 git reset --hard commit_id 退到/进 ...

  10. Xbuild

    https://github.com/zhuayi/xbuild