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. 使用 QSqlTableModel 模型向数据库中插入数据时,为什么使用 rowCount 函数只能返回 256 最大值?

    默认返回缓冲区里面的数据,如果你向要获取更多值,请在前面加入以下语句即可. while(model.canFetchMore()){ model.fetchMore(); } 该语句会获取更多的记录.

  2. Https socket 连接

    介: 本文主要介绍了网络安全通讯协议 SSL/TLS 和 Java 中关于安全通讯的实现部分.并通过一个简单的样例程序实现,来展示如何在 Java 平台上正确建立安全通讯. 在人类建立了通信系统之后, ...

  3. ASP.NET Core 2.1 JWT token (一) - 简书

    原文:ASP.NET Core 2.1 JWT token (一) - 简书 JwtBearer认证是一种标准的,通用的,无状态的,与语言无关的认证方式.Bearer验证属于HTTP协议标准验证. 如 ...

  4. 八、hibernate的查询(HQL)

    HQL:Hibernate Query Language 提供更加丰富灵活.更为强大的查询能力 HQL更接近SQL语句查询语法 面向对象的查询 "from Children where ci ...

  5. document.location window.location

    document.location 和 window.location 取url的值的时候可以通用,但是 document是window的属性,所以不能直接用document.location =ur ...

  6. python杂货

    三.字典的基本操作 1.如何访问字典中的值? adict[key] 形式返回键key对应的值value,如果key不在字典中会引发一个KeyError. adict.get(key, default ...

  7. Centos7下源码编译安装与配置redis5.0

    1.下载redis5.0源码包 wget http://download.redis.io/releases/redis-5.0.5.tar.gz 2.检查是否安装过之前的历史版本 rpm -qa|g ...

  8. mysql基于GTIDS复制

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

  9. UVA1411 Ants

    想出的一道题竟然是原题QAQ 非常有趣的一个题 根据三角形两边之和大于第三边 所以相交的线段一定是比不相交的线段要长的 所以直接二分图构图 最小费用最大流即可 (我不管我不管我要把这个出到NOIP膜你 ...

  10. 【leetcode】931. Minimum Falling Path Sum

    题目如下: Given a square array of integers A, we want the minimum sum of a falling path through A. A fal ...