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

Notice

The subarray should contain at least one number.

Have you met this question in a real interview?

Yes
Example

Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.

Challenge

Can you do it in time complexity O(n)?

LeetCode上的原题,请参见我之前的博客Maximum Subarray

解法一:

  1. class Solution {
  2. public:
  3. /**
  4. * @param nums: A list of integers
  5. * @return: A integer indicate the sum of max subarray
  6. */
  7. int maxSubArray(vector<int> nums) {
  8. int res = INT_MIN, curSum = ;
  9. for (int num : nums) {
  10. curSum += num;
  11. curSum = max(curSum, num);
  12. res = max(res, curSum);
  13. }
  14. return res;
  15. }
  16. };

解法二:

  1. class Solution {
  2. public:
  3. /**
  4. * @param nums: A list of integers
  5. * @return: A integer indicate the sum of max subarray
  6. */
  7. int maxSubArray(vector<int> nums) {
  8. if (nums.empty()) return ;
  9. return helper(nums, , (int)nums.size() - );
  10. }
  11. int helper(vector<int>& nums, int left, int right) {
  12. if (left >= right) return nums[left];
  13. int mid = left + (right - left) / ;
  14. int lmax = helper(nums, left, mid - );
  15. int rmax = helper(nums, mid + , right);
  16. int mmax = nums[mid], t = mmax;
  17. for (int i = mid - ; i >= left; --i) {
  18. t += nums[i];
  19. mmax = max(mmax, t);
  20. }
  21. t = mmax;
  22. for (int i = mid + ; i <= right; ++i) {
  23. t += nums[i];
  24. mmax = max(mmax, t);
  25. }
  26. return max(mmax, max(lmax, rmax));
  27. }
  28. };

[LintCode] Maximum Subarray 最大子数组的更多相关文章

  1. [LeetCode] Maximum Subarray 最大子数组

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

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

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

  3. [leetcode]53. Maximum Subarray最大子数组和

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

  4. [LeetCode] 53. Maximum Subarray 最大子数组 --动态规划+分治

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

  5. 【LeetCode每天一题】Maximum Subarray(最大子数组)

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

  6. Maximum Subarray(最大子数组)

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

  7. LintCode: Maximum Subarray

    1. 暴力枚举 2. “聪明”枚举 3. 分治法 分:两个基本等长的子数组,分别求解T(n/2) 合:跨中心点的最大子数组合(枚举)O(n) 时间复杂度:O(n*logn) class Solutio ...

  8. [Leetcode] maximun subarray 最大子数组

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

  9. 【LeetCode】53. Maximum Subarray 最大子序和 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力解法 动态规划 日期 题目地址: https:/ ...

随机推荐

  1. 463. Island Perimeter

    https://leetcode.com/problems/island-perimeter/ 在一个N×N的矩阵中,N<100,1代表岛,0代表海,岛内没有海,求岛的周长 [[0,1,0,0] ...

  2. B+Tree和MySQL索引分析

    首先区分两组概念: 稠密索引,稀疏索引: 聚簇索引,非聚簇索引: btree和mysql的分析: 参见 http://blog.csdn.net/hguisu/article/details/7786 ...

  3. ajax用get刷新页面元素在IE下无效解决~~

    总结一下解决办法: 在IE下用Ajax请求某一页面,通常会因为缓存的原因而返回上一次的结果,造成混乱,[即get方式时,获取数据,因发送参数和地址都一致,故IE浏览器会从缓存中取,而不会去请求服务器端 ...

  4. MySQL存储过程动态SQL语句的生成

    用Mysql存储过程来完成动态SQL语句,使用存储过程有很好的执行效率: 现在有要求如下:根据输入的年份.国家.节假日类型查询一个节假日,我们可以使用一般的SQL语句嵌入到Java代码中,但是执行效率 ...

  5. js中的斐波那契数列法

    //斐波那契数列:1,2,3,5,8,13…… //从第3个起的第n个等于前两个之和 //解法1: var n1 = 1,n2 = 2; for(var i=3;i<101;i++){ var ...

  6. JS脚本语言是什么意思?

    javascript,Javascript是一种浏览器端的脚本语言,用来在网页客户端处理与用户的交互,以及实现页面特效.比如提交表单前先验证数据合法性,减少服务器错误和压力.根据客户操作,给出一些提升 ...

  7. SQL注入以及如何防止和索引

    SQL注入产生的原因:程序开发过程中不注意规范书写sql语句和对特殊字符进行过滤,导致客户端可以通过全局变量POST和GET提交一些sql语句正常执行. 防止SQL注入: 1.开启配置文件中的magi ...

  8. 1250 Super Fast Fourier Transform(湘潭邀请赛 暴力 思维)

    湘潭邀请赛的一题,名字叫"超级FFT"最终暴力就行,还是思维不够灵活,要吸取教训. 由于每组数据总量只有1e5这个级别,和不超过1e6,故先预处理再暴力即可. #include&l ...

  9. Remove Duplicates from Sorted Array II

    题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...

  10. 前端项目通用、常用js common.js

    var url = location.href; if (url.toLowerCase().indexOf("/akweb_admin/") == -1) { function ...