lc 53 Maximum Subarray


53 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],

the contiguous subarray [4,-1,2,1] has the largest sum = 6.

方法一

首先想到的就是比较暴力,没有技巧性可言的时间复杂度为O(\(n^{2}\))的方法。显而易见,这个方法重复计算了很多,非常没有效率。

int maxSubArray(int* nums, int numsSize) {
int max = nums[0];
for (int i = 0; i < numsSize; i++) {
int sum = nums[i];
if (sum >= max) max = sum;
for (int j = i+1; j < numsSize; j++) {
if (max <= sum+nums[j])
max = sum+nums[j];
sum += nums[j];
}
}
return max;
}

方法二

这道题目还可以利用动态规划的算法,通过维护全局最优变量和局部最优变量,局部最优是一定要包含当前元素,local = (local < 0) ? nums[i] : local+nums[i];。有了当前一步的局部最优,那么全局最优就是当前的局部最优或者还是原来的全局最优,global = (local > global) ? local : global;。

这个动态规划方法的时间复杂度可以降低到O(n),可以说是非常acceptable了。

int maxSubArray(int* nums, int numsSize) {
int global = nums[0], local = nums[0];
for(int i = 1; i < numsSize; i++) {
local = (local < 0) ? nums[i] : local+nums[i];
global = (local > global) ? local : global;
}
return global;
}

LN : leetcode 53 Maximum Subarray的更多相关文章

  1. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  2. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

    原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...

  3. 41. leetcode 53. Maximum Subarray

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

  4. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  5. leetcode 53. Maximum Subarray 、152. Maximum Product Subarray

    53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...

  6. LeetCode 53. Maximum Subarray(最大的子数组)

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

  7. leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

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

  8. [LeetCode] 53. Maximum Subarray 最大子数组

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  9. C#解leetcode 53.Maximum Subarray

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

随机推荐

  1. IE11 文档模式空白

    环境描述: win7 64位系统 安装了 更新 IE11-Windows6.1-KB2929437-x64.IE11-Windows6.1-KB3008923-x64 解决方案: 卸载 IE11-Wi ...

  2. 请问这个「 (?<=<(\w+)>).*(?=<\/\1>) 」正则表达式是什么意思呢?

    问题:https://www.zhihu.com/question/26480812  (?<=<(\w+)>).*(?=<\/\1>) ---------------- ...

  3. 【转】Linux软连接和硬链接

    再次温习一下,操作的不多.虽然感觉都会!!!! 这次再次操作一遍!! 通过上面的测试发现,删除f1之后,软连接f3就无效了,硬链接f3则不受影响. ls -F可以看到文件的类型. 连接文件的作用? - ...

  4. Java之旅hibernate(2)——文件夹结构

    Hibernate的jar最好是到官网上进行下载.下载最新的稳定的版本号.之后进行解压,以下我们介绍一下hibernate的包结构. 1.      包结构 我们能够看到包文件夹结构发生了变化.我以5 ...

  5. Mahout贝叶斯算法拓展篇3---分类无标签数据

    代码測试环境:Hadoop2.4+Mahout1.0 前面博客:mahout贝叶斯算法开发思路(拓展篇)1和mahout贝叶斯算法开发思路(拓展篇)2 分析了Mahout中贝叶斯算法针对数值型数据的处 ...

  6. HDU 1226 超级password

    跟POJ 1465 multiple 类是.仅仅只是多了2个条件,长度不能超过500.还有就是 可能不是十进制. bfs+同余定理,就是用 mod 来判重. G++ 15ms 每次枚举一位,然后记录下 ...

  7. jquery.validate自己定义验证--成功提示与择要提示

    1. 自己定义验证--成功提示 1) 加入选项 errorClass: "unchecked". validClass: "checked", errorEle ...

  8. Python 不同对象比較大小

    万恶的源泉: Fireboo的疑问(当然 lambda 本身写的就有问题): >>> filter( lambda x: x > 2, [ 1, [ 1, 2, 3 ], 2, ...

  9. Application Warm-up Module IIS7.5 也有Warm Up功能,让ASP.NET 第一次Request不变慢

    Application Warm-up Module: 應用程式的暖機代理人 http://www.microsoft.com/taiwan/technet/iis/expand/Applicatio ...

  10. YTU 2635: P4 游戏中的Human角色

    2635: P4 游戏中的Human角色 时间限制: 1 Sec  内存限制: 128 MB 提交: 524  解决: 328 题目描述 在一个平面打斗游戏中,任何的角色(Role)都有血量(bloo ...