leetcode || 53、Maximum Subarray
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
.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
thinking:
(1)这道题解法特别多:
方法1:将每个数和后一个数字相加,得到一个正负分布的序列,正数项对最大和子序列实用,作比較就可以
方法2:暴力匹配,两层循环,调用max()函数。时间复杂度O(n*n),也能够算出结果,可是提交超时
方法3:採用DP。时间复杂度O(n)
方法4:分治法。时间复杂度为nlog(n)
(2)本人实现了方法3 和方法4
code:
DP 法:
- class Solution {
- public:
- int maxSubArray(int A[], int n) {
- int sum=A[0];
- int maxsum=A[0];
- for(int i=1;i<n;i++)
- {
- if(sum<0) //DP核心
- sum=0;
- sum+=A[i];
- maxsum=max(sum,maxsum);
- }
- return maxsum;
- }
- };
分治法:
- class Solution {
- public:
- int maxSubArray(int A[], int n) {
- int ret=maxsub(A,0,n-1);
- return ret;
- }
- protected:
- int maxsub(int A[], int start, int end)
- {
- int max_left=INT_MIN,max_mid=INT_MIN,max_right=INT_MIN;
- if(start==end)
- return A[start];
- if(start+1==end)
- {
- int a=max(A[start],A[end]);
- return a>(A[start]+A[end])?a:(A[start]+A[end]);
- }
- int mid=(start+end)/2;
- int tmp_sum=A[mid];
- max_mid=tmp_sum;
- int i=mid-1;
- int j=mid+1;
- while(i>=start) //难点在于当连续最大和子序列分布在mid的一側或两側时,怎么处理
- {
- tmp_sum+=A[i];
- i--;
- max_mid=max(max_mid,tmp_sum);
- }
- if(max_mid>A[mid]) //推断是处于两側,还是处于一側
- tmp_sum=max_mid;
- else
- tmp_sum=A[mid];
- while(j<=end)
- {
- tmp_sum+=A[j];
- j++;
- max_mid=max(max_mid,tmp_sum);
- }
- max_left=max(max_left,maxsub(A,start,mid-1));//二分轮廓
- max_right=max(max_right,maxsub(A,mid+1,end));
- int tmp_max = max(max_left,max_right);
- return max_mid>tmp_max?max_mid:tmp_max;
- }
- };
leetcode || 53、Maximum Subarray的更多相关文章
- (LeetCode 53)Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【LeetCode】053. Maximum Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- [LeetCode]题53:Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- LeetCode(53) Maximum Subarray
题目 Find the contiguous subarray within an array (containing at least one number) which has the large ...
- 【LeetCode算法-53】Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- 【算法】LeetCode算法题-Maximum Subarray
这是悦乐书的第154次更新,第156篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第13题(顺位题号是53).给定一个整数数组nums,找出一个最大和,此和是由数组中索引 ...
- 【leetcode】1186. Maximum Subarray Sum with One Deletion
题目如下: Given an array of integers, return the maximum sum for a non-empty subarray (contiguous elemen ...
- LeetCode OJ:Maximum Subarray(子数组最大值)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- leetcode 53. Maximum Subarray 、152. Maximum Product Subarray
53. Maximum Subarray 之前的值小于0就不加了.dp[i]表示以i结尾当前的最大和,所以需要用一个变量保存最大值. 动态规划的方法: class Solution { public: ...
随机推荐
- ECshop 二次开发模板教程2
不知道大家是学会用循环了呢,还是我的言语实在有问题,大家实在无法完成阅读哦,居然大家都没有问题,暂时心里安慰,把他当做好事情,大家都会调用了,呵呵,那我们继续循环调用商品了!好,继续在我们昨天的基础上 ...
- 22个所见即所得在线 Web 编辑器
前言: 关于编辑器,适合的才是最好的,接下来,我会写一些关于日志编辑器的文章,今天就写写,可能内容会比较多. --------------------------------------------- ...
- AppServ的安装与配置
AppServ是一个软件集合,包括Apache(HTTP服务器软件).PHP(网页程序设计语言).MySQL(数据库管理系统软件).phpMyAdmin(图形界面的数据库管理软件)四个组成部分.App ...
- duilib中控件拖拽功能的实现方法(附源码)
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/41144283 duilib库中原本没有显示的对控件增加拖拽的功能,而实际 ...
- Canvas入门(1):绘制矩形、圆、直线、曲线等基本图形
来源:http://www.ido321.com/968.html 一.Canvas的基础知识 Canvas是HTML 5中新增的元素,专门用于绘制图形.canvas元素就相当于一块“画布”,一块无色 ...
- web服务器分析与设计(五)--一些总结
随着年龄与经验的增加,对于软件方面的分析与设计也会有一些新的认识.下面做个近期的总结: 1,关于到底用不用作设计的问题: 在最近两个公司,原有人马是不会作设计(我自己的感觉),也察觉不到作设计的任何冲 ...
- 用Windows Live Writer发来
文字 package com.myeclipseide.example.myblog.secure; import com.opensymphony.xwork2.ActionSupport; ...
- 用一个例子学习CSS的伪类元素
CSS伪类元素是一个非常酷的东西!首先我们理解一下它,:before :after 伪类元素,也就是虚假的元素.它可以插入在元素的前面或者后面,而在HTML文档结构中,它却是不存在的,因为Js是无法通 ...
- JavaSE聊天室
今天学习了一下简单聊天程序(类似QQ那种)的编写过程,从最初的0.1版本到最后的1.3版本,功能不断地增强,下面对一天的学习内容进行梳理. 版本0.1 我们的需求是显示一个窗体,其他什么也不用做,其他 ...
- Codeforces 622B The Time 【水题】
B. The Time time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...