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. java面试题-看到那记录到哪

    哈希冲突 如果两个不同的元素,通过哈希函数得到的实际存储地址相同怎么办?也就是说,当我们对某个元素进行哈希运算,得到一个存储地址,然后要进行插入的时候,发现已经被其他元素占用了,其实这就是所谓的哈希冲 ...

  2. Java关于线程池的使用

    一.四种线程池创建的方式 Java通过Executors提供四种线程池,分别为: newCachedThreadPool 创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回 ...

  3. Nginx+Keepalived主从配置(双机主从热备)+Tomcat集群

    拓扑环境 以下表格是这次測试须要的拓扑环境,几台server.每台server上安装什么,都有介绍. server名称 系统版本号 预装软件 IP地址/VIP Nginx主server CentOS ...

  4. 【记录】STS设置maven远程仓库,加快下载jar包

    在STS中windows->preferences->maven->user settings 修改user settings里setting.xml配置文件 <?xml ve ...

  5. 2019-11-7-WPF-How-to-get-plain-text-from-RichTextBox

    title author date CreateTime categories WPF How to get plain text from RichTextBox lindexi 2019-11-0 ...

  6. shell条件测试举例

  7. Ubuntu14.04搭建Boa服务

    1. 下载 boa 源码 : https://sourceforge.net/projects/boa/ 版本:boa-0.94.13.tar.gz 2. 在Ubuntu 下解压进入 [boa-0.0 ...

  8. mysql基于GTIDS复制

    GTIDS的环境:一台master 192.168.200.111多个slave: 192.168.200.112 192.168.200.113 修改master服务器:[root@localhos ...

  9. 力扣 -- 寻找两个有序数组的中位数 Median of Two Sorted Arrays python实现

    题目描述: 中文: 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums ...

  10. docker 运行springboot jar包

    1.将jar包移至自定义的/usr/jar目录下; 2.在/usr/jar目录下创建Dockerfile文件 文件如下: #FROM命令定义构建镜像的基础镜像,该条必须是dockerfile的首个命令 ...