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. Ubuntu 16.04利用SecureCRT上传/下载文件(sz/rz命令)

    说明:XShell同样也是支持的. 一.安装软件 sudo apt-get install lrzsz 二.sz下载文件用法: #下载一个文件 sz filename #下载多个文件 sz filen ...

  2. Vue.js父子通信之所有方法和数据共享

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. laravel有用的方法

    1.tinker 造假数据 factory('App\User',3)->create(); DB::table 返回collection,可以用collection中的很多方法 比如-> ...

  4. C#的DataGridView如何修改字体

    在RowTemplate中可以修改字体和显示的格式(比如保留几位小数)                  

  5. C# .NET想要另存一个项目,sln文件丢了怎么办

    如下图所示,我想要另存一个工程,把 V4.4整个的项目另存为V4.5,我可以把解决方案文件(.sln)改名字,但是我没法把文件夹改名字,改了打开sln就说找不到.   很简单的一个思路是反正sln是多 ...

  6. Android学习笔记-保存数据的实现方法2-SharedPreferences

    Android下,数据的保存,前面介绍过了,把数据保存到内存以及SD卡上,这次我们就介绍一下,更为常用的采用SharedPreferences的方式来保存数据, 1,得到SharedPreferenc ...

  7. Codeforces(429D - Tricky Function)近期点对问题

    D. Tricky Function time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. JBOSS和EJB学习一

    1.使用软件 IDE:Eclipse4.3(开普勒) EE版本 服务器:jboss EAP 6.2 eclipse-jboss plugin:jbosstools-Update-4.1.2.Final ...

  9. Copy Selected Text from any window

    https://social.msdn.microsoft.com/Forums/windows/en-US/1dc356e6-9441-44de-9eda-247003fa6ef5/copy-sel ...

  10. ASP.NET快速开发框架之工作流引擎

    大家好,啊!我小六六也有自己的博客了,今天我来跟大家分享下我的撸码心得,顺便吐槽下,我的坑爹上司.作为一名程序猿,我在系统开发方面也是“浸淫”了许久了!喔,不不不,是经营! 今天我就跟大家分享一下工作 ...