problem:

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.

click to show more practice.

More practice:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

Hide Tags

Divide and Conquer Array Dynamic
Programming

题意:找出一个序列的最大和连续子序列

thinking:

(1)这道题解法特别多:

方法1:将每个数和后一个数字相加,得到一个正负分布的序列,正数项对最大和子序列实用,作比較就可以

方法2:暴力匹配,两层循环,调用max()函数。时间复杂度O(n*n),也能够算出结果,可是提交超时

方法3:採用DP。时间复杂度O(n)

方法4:分治法。时间复杂度为nlog(n)

(2)本人实现了方法3 和方法4

code:

DP 法:

  1. class Solution {
  2. public:
  3. int maxSubArray(int A[], int n) {
  4. int sum=A[0];
  5. int maxsum=A[0];
  6. for(int i=1;i<n;i++)
  7. {
  8. if(sum<0) //DP核心
  9. sum=0;
  10. sum+=A[i];
  11. maxsum=max(sum,maxsum);
  12. }
  13. return maxsum;
  14. }
  15.  
  16. };

分治法:

  1. class Solution {
  2. public:
  3. int maxSubArray(int A[], int n) {
  4. int ret=maxsub(A,0,n-1);
  5. return ret;
  6.  
  7. }
  8. protected:
  9. int maxsub(int A[], int start, int end)
  10. {
  11. int max_left=INT_MIN,max_mid=INT_MIN,max_right=INT_MIN;
  12. if(start==end)
  13. return A[start];
  14. if(start+1==end)
  15. {
  16. int a=max(A[start],A[end]);
  17. return a>(A[start]+A[end])?a:(A[start]+A[end]);
  18. }
  19. int mid=(start+end)/2;
  20. int tmp_sum=A[mid];
  21. max_mid=tmp_sum;
  22.  
  23. int i=mid-1;
  24. int j=mid+1;
  25. while(i>=start) //难点在于当连续最大和子序列分布在mid的一側或两側时,怎么处理
  26. {
  27.  
  28. tmp_sum+=A[i];
  29. i--;
  30. max_mid=max(max_mid,tmp_sum);
  31.  
  32. }
  33. if(max_mid>A[mid]) //推断是处于两側,还是处于一側
  34. tmp_sum=max_mid;
  35. else
  36. tmp_sum=A[mid];
  37. while(j<=end)
  38. {
  39.  
  40. tmp_sum+=A[j];
  41. j++;
  42. max_mid=max(max_mid,tmp_sum);
  43.  
  44. }
  45.  
  46. max_left=max(max_left,maxsub(A,start,mid-1));//二分轮廓
  47. max_right=max(max_right,maxsub(A,mid+1,end));
  48. int tmp_max = max(max_left,max_right);
  49. return max_mid>tmp_max?max_mid:tmp_max;
  50. }
  51.  
  52. };

leetcode || 53、Maximum Subarray的更多相关文章

  1. (LeetCode 53)Maximum Subarray

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

  2. 【LeetCode】053. Maximum Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  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

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

  5. 【LeetCode算法-53】Maximum Subarray

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

  6. 【算法】LeetCode算法题-Maximum Subarray

    这是悦乐书的第154次更新,第156篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第13题(顺位题号是53).给定一个整数数组nums,找出一个最大和,此和是由数组中索引 ...

  7. 【leetcode】1186. Maximum Subarray Sum with One Deletion

    题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...

  8. LeetCode OJ:Maximum Subarray(子数组最大值)

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

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

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

随机推荐

  1. ECshop 二次开发模板教程2

    不知道大家是学会用循环了呢,还是我的言语实在有问题,大家实在无法完成阅读哦,居然大家都没有问题,暂时心里安慰,把他当做好事情,大家都会调用了,呵呵,那我们继续循环调用商品了!好,继续在我们昨天的基础上 ...

  2. 22个所见即所得在线 Web 编辑器

    前言: 关于编辑器,适合的才是最好的,接下来,我会写一些关于日志编辑器的文章,今天就写写,可能内容会比较多. --------------------------------------------- ...

  3. AppServ的安装与配置

    AppServ是一个软件集合,包括Apache(HTTP服务器软件).PHP(网页程序设计语言).MySQL(数据库管理系统软件).phpMyAdmin(图形界面的数据库管理软件)四个组成部分.App ...

  4. duilib中控件拖拽功能的实现方法(附源码)

    转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41144283 duilib库中原本没有显示的对控件增加拖拽的功能,而实际 ...

  5. Canvas入门(1):绘制矩形、圆、直线、曲线等基本图形

    来源:http://www.ido321.com/968.html 一.Canvas的基础知识 Canvas是HTML 5中新增的元素,专门用于绘制图形.canvas元素就相当于一块“画布”,一块无色 ...

  6. web服务器分析与设计(五)--一些总结

    随着年龄与经验的增加,对于软件方面的分析与设计也会有一些新的认识.下面做个近期的总结: 1,关于到底用不用作设计的问题: 在最近两个公司,原有人马是不会作设计(我自己的感觉),也察觉不到作设计的任何冲 ...

  7. 用Windows Live Writer发来

    文字     package com.myeclipseide.example.myblog.secure; import com.opensymphony.xwork2.ActionSupport; ...

  8. 用一个例子学习CSS的伪类元素

    CSS伪类元素是一个非常酷的东西!首先我们理解一下它,:before :after 伪类元素,也就是虚假的元素.它可以插入在元素的前面或者后面,而在HTML文档结构中,它却是不存在的,因为Js是无法通 ...

  9. JavaSE聊天室

    今天学习了一下简单聊天程序(类似QQ那种)的编写过程,从最初的0.1版本到最后的1.3版本,功能不断地增强,下面对一天的学习内容进行梳理. 版本0.1 我们的需求是显示一个窗体,其他什么也不用做,其他 ...

  10. Codeforces 622B The Time 【水题】

    B. The Time time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...