LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-,,-,,-,,,-,],
Output:
Explanation: [,-,,] has the largest sum = .
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
我的解法:两层循环 第一层循环通过i控制进度,第二层循环计算从i 开始的子数组的和的最大值
C#版解法:
public class Solution {
public int MaxSubArray(int[] nums) {
if(nums.Length == )
return ;
int max = int.MinValue;
for(int i = ; i < nums.Length; i++){
int tempSum=;
for(int j =i; j < nums.Length; j++){
tempSum += nums[j];
if(tempSum > max)
max = tempSum;
}
}
return max;
}
}
看题目描述,可以使用divide and conquer(分而治之)思想去实现,时间复杂度会大幅度降低:这里先将解法贴出来,具体的分而治之(动态规划)的学习放到下一个文章中
static int maxCrossingSum(int[] arr, int l,
int m, int h)
{
// Include elements on left of mid.
int sum = ;
int left_sum = int.MinValue;
for (int i = m; i >= l; i--)
{
sum = sum + arr[i];
if (sum > left_sum)
left_sum = sum;
} // Include elements on right of mid
sum = ;
int right_sum = int.MinValue; ;
for (int i = m + ; i <= h; i++)
{
sum = sum + arr[i];
if (sum > right_sum)
right_sum = sum;
} // Return sum of elements on left
// and right of mid
return left_sum + right_sum;
} // Returns sum of maxium sum subarray
// in aa[l..h]
static int maxSubArraySum(int[] arr, int l,
int h)
{ // Base Case: Only one element
if (l == h)
return arr[l]; // Find middle point
int m = (l + h) / ; /* Return maximum of following three
possible cases:
a) Maximum subarray sum in left half
b) Maximum subarray sum in right half
c) Maximum subarray sum such that the
subarray crosses the midpoint */
return Math.Max(Math.Max(maxSubArraySum(arr, l, m),
maxSubArraySum(arr, m + , h)),
maxCrossingSum(arr, l, m, h));
} /* Driver program to test maxSubArraySum */
public static void Main()
{
int[] arr = { -, , , -, };
int n = arr.Length;
int max_sum = maxSubArraySum(arr, , n - ); Console.Write("Maximum contiguous sum is " +
max_sum);
Console.ReadKey();
}
分而治之的链接:算法学习 分而治之思想
LeetCode Array Easy 53. Maximum Subarray 个人解法 和分治思想的学习的更多相关文章
- LeetCode练题——53. Maximum Subarray
1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...
- [LeetCode&Python] Problem 53. Maximum Subarray
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
- [array] leetcode - 53. Maximum Subarray - Easy
leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...
- Leetcode之53. Maximum Subarray Easy
Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...
- [Leetcode][Python]53: Maximum Subarray
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- Leetcode#53.Maximum Subarray(最大子序和)
题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...
- 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略
原题 Given an integer array nums, find the contiguous subarray (containing at least one number) which ...
- 41. leetcode 53. Maximum Subarray
53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...
随机推荐
- ArcGis基础——相接面制造指定距离的分隔带
回家,出发前夜,看完电影吃晚饭回到住处已近十一点,和同事扯了一会儿淡,正准备去睡觉,这哥们儿突然想起一个问题: 如何把相接的面搞出一个20cm的分隔带?因为两区划定项目数据质检要求不同的地块图斑间应有 ...
- Python之实现迭代器协议
什么是迭代器: --迭代器(迭代就是循环) 可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator 可迭代对象有: 一类是集合数据类型,如list,tuple,dict,set ...
- 轻量级Spring定时任务(Spring-task)
Spring3.0以后自主开发的定时任务工具,spring-task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式. ...
- 转载:对比Angular/jQueryUI/Extjs:没有一个框架是万能的
Angular不能做什么?对比Angular/jQueryUI/Extjs 框架就好比兵器,你得明白你手里拿的是屠龙刀还是倚天剑,刀法主要是砍,剑法主要是刺.对于那些职业喷子和脑残粉,小僧送你们两个字 ...
- 三、Centos7安装Mysql
1.到服务器下载的链接 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2.执行命令 sudo r ...
- 使用CSS3的@media来编写响应式的页面
首先要知道,我们为什么要写自适应的页面(响应式页面) [直接看干货] 众所周知,电脑.平板.手机的屏幕是差距很大的,假如在电脑上写好了一个页面,在电脑上看起来不错,但是如果放到手机上的话,那可能就会乱 ...
- Android开发文档
https://developer.android.com/ 用ke学上网方能打开
- 理解Java主函数中的"String[] args"
public class Understand_String_args { public static void main(String[] args) { System.out.printf(&qu ...
- UNP学习 非阻塞I/O
缺省状态下,套接口时阻塞方式的.这意味着当一个套接口调用不能立即完成时,进程进入睡眠状态,等待操作完成.我们将可能阻塞的套接口调用分成四种. 1.输入操作:read.readv.recv.recvfr ...
- 匹配Luhn算法:可用于检测银行卡卡号
匹配Luhn算法:可用于检测银行卡卡号 /** * http://www.cnblogs.com/JnKindle/p/5798974.html * * 匹配Luhn算法:可用于检测银行卡卡号 * * ...