Description

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 个人解法 和分治思想的学习的更多相关文章

  1. LeetCode练题——53. Maximum Subarray

    1.题目 53. Maximum Subarray——Easy Given an integer array nums, find the contiguous subarray (containin ...

  2. [LeetCode&Python] Problem 53. Maximum Subarray

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

  3. [array] leetcode - 53. Maximum Subarray - Easy

    leetcode - 53. Maximum Subarray - Easy descrition Find the contiguous subarray within an array (cont ...

  4. Leetcode之53. Maximum Subarray Easy

    Leetcode 53 Maximum Subarray Easyhttps://leetcode.com/problems/maximum-subarray/Given an integer arr ...

  5. [Leetcode][Python]53: Maximum Subarray

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 53: Maximum Subarrayhttps://leetcode.co ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. Leetcode#53.Maximum Subarray(最大子序和)

    题目描述 给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大. 例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4], 连续子序列 [4,-1,2,1] ...

  8. 小旭讲解 LeetCode 53. Maximum Subarray 动态规划 分治策略

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

  9. 41. leetcode 53. Maximum Subarray

    53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) w ...

随机推荐

  1. Centos设置自带中文输入法

    1.点击屏幕左上角的[系统应用程序]->[系统工具]->[设置] 2.在弹出的设置对话窗中选择Region&language 3.在弹出的[区域和语言]对话窗中,点击左下角“+”, ...

  2. setlocale - 设置当前的区域选项

    总览 (SYNOPSIS) #include <locale.h> char *setlocale(int category, const char * locale); 描述 (DESC ...

  3. dubbo服务调试管理实用命令

    公司如果分项目组开发的,各个项目组调用各项目组的接口,有时候需要在联调环境调试对方的接口,可以直接telnet到dubbo的服务通过命令查看已经布的接口和方法,并能直接invoke具体的方法,我们可以 ...

  4. springboot基于注解动态配置多数据源以及多数据源的事务统一

    参考文档:https://www.cnblogs.com/zhangboyu/p/7622412.html https://blog.csdn.net/qq_34322777/article/deta ...

  5. Oracle 19C的下载和安装部署

    1.官网下载zip包. 2.解压到/usr/local/oracle 目录. 3.创建用户和用户组 /usr/sbin/useradd -u oracle //用户组oracle /usr/sbin/ ...

  6. 关于计算机与MCU通信及MAX232、CH340T与PL2303的区别

    原理:单片机的TXD.RXD是TTL电平,所以你得万变不离其宗的将其它信号转成TTL电平,只有这样给单片机下载程序才有可能成功!其中CH340.PL2303等芯片是直接将USB信号转换为TTL电平或者 ...

  7. WGCNA构建基因共表达网络详细教程

    这篇文章更多的是对于混乱的中文资源的梳理,并补充了一些没有提到的重要参数,希望大家不会踩坑. 1. 简介 1.1 背景 WGCNA(weighted gene co-expression networ ...

  8. (urls.E006) The MEDIA_URL setting must end with a slash. (urls.e006)

    这个错误是会害死人的!   repath 的使用 setting的配置:在末尾要加一个斜杠 models urls的配置

  9. 【算法】一致性Hash算法

    一.分布式算法 在做服务器负载均衡时候可供选择的负载均衡的算法有很多,包括: 轮循算法(Round Robin).哈希算法(HASH).最少连接算法(Least Connection).响应速度算法( ...

  10. padding 填充

    CSS padding(填充)是一个简写属性,定义元素边框与元素内容之间的空间,即上下左右的内边距. padding(填充) 当元素的 padding(填充)内边距被清除时,所释放的区域将会受到元素背 ...