问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4012 访问。

给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。

输入: [-2,1,-3,4,-1,2,1,-5,4],

输出: 6

解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。

进阶:

如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。


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

Input: [-2,1,-3,4,-1,2,1,-5,4],

Output: 6

Explanation: [4,-1,2,1] has the largest sum = 6.

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.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4012 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = { -2, 1, -3, 4, -1, 2, 1, -5, 4 }; var res = maxSubArray(nums);
Console.WriteLine(res); nums = new int[] { 4, 1, 0, -3, -1, 9, 1 }; res = maxSubArray2(nums);
Console.WriteLine(res); Console.ReadKey();
} private static int maxSubArray(int[] nums) {
//常规写法
//记录最大和、本次可以“接受”的和(本次和)
var max = int.MinValue;
var thisSum = 0;
for(var i = 0; i < nums.Length; i++) {
//如果和小于等于0,那么当前值没有用,应当舍去
//(为了优化写法,其实是上一次循环时的和)
//即重置本次和为当前值
if(thisSum <= 0) {
thisSum = nums[i];
} else {
//否则,迭加和
thisSum += nums[i];
}
//用max记录更大的值
if(max < thisSum) {
max = thisSum;
}
}
//返回最大值
return max;
} private static int maxSubArray2(int[] nums) {
//高度精简优化写法
var max = int.MinValue;
var thisSum = 0;
for(var i = 0; i < nums.Length; i++) {
max = Math.Max(max, thisSum = Math.Max(nums[i], thisSum + nums[i]));
}
return max;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4012 访问。

6
11

分析:

显而易见,以上2种算法的时间复杂度均为:  。

C#LeetCode刷题之#53-最大子序和(Maximum Subarray)的更多相关文章

  1. LeetCode 53. 最大子序和(Maximum Subarray)

    53. 最大子序和 53. Maximum Subarray 题目描述 给定一个整数数组 nums,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. LeetCode53. M ...

  2. 【leetcode算法-简单】53. 最大子序和

    [题目描述] 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释:  ...

  3. C#LeetCode刷题之#559-N叉树的最大深度​​​​​​​(Maximum Depth of N-ary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4088 访问. 给定一个 N 叉树,找到其最大深度. 最大深度是指 ...

  4. C#LeetCode刷题之#104-二叉树的最大深度​​​​​​​(Maximum Depth of Binary Tree)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4072 访问. 给定一个二叉树,找出其最大深度. 二叉树的深度为根 ...

  5. leetcode刷题-94二叉树的中序遍历

    题目 给定一个二叉树,返回它的中序 遍历. 实现 # def __init__(self, x): # self.val = x # self.left = None # self.right = N ...

  6. [Swift]LeetCode53. 最大子序和 | Maximum Subarray

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

  7. C#LeetCode刷题-树

    树篇 # 题名 刷题 通过率 难度 94 二叉树的中序遍历   61.6% 中等 95 不同的二叉搜索树 II   43.4% 中等 96 不同的二叉搜索树   51.6% 中等 98 验证二叉搜索树 ...

  8. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  9. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串   22.4% 中等 10 正则表达式匹配   18.8% 困难 32 最长有效括号   23.3% 困难 44 通配符匹配   17.7% ...

  10. C#LeetCode刷题-分治算法

    分治算法篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...

随机推荐

  1. Cyber Security - Palo Alto Firewall Objects Addresses, Services, and Groups(1)

    Address Objects and Groups Creating address objects. Organizing address objects with address groups ...

  2. Python Ethical Hacking - The Lab and Needed Software

    The Lab and Needed Software Attacker Machine - Kali Linux https://www.kali.org/ 1. Install the softw ...

  3. 题解 洛谷 P5303 【[GXOI/GZOI2019]逼死强迫症】

    可以先去考虑没有\(1 \times 1\)的砖块的情况,对于最后一个位置只有两种情况,一个是竖着用一块砖铺设\(2 \times 1\),另一个为横着用两块砖铺设\(2 \times 2\). 设没 ...

  4. xilinx fpga中块ram的使用——简单双端口ram的使用

    在简单双端口ram中最简单有9个端口:分别是 clka  为输入端口的时钟 wea  读写控制端,高为写,低为读 addra 写地址 dina  待写入的数据 clkb 为输出端口的时钟的 addrb ...

  5. 从Python开始学编程|PDF百度网盘免费下载|Python新手入门

    百度网盘免费下载:从Python开始学编程|附PDF免费下载 提取码:7nkf 豆瓣评分: 本书封面: 读者评论: 内容简介  · · · · · · 改编自Vamei博客的<Python快速教 ...

  6. Python延迟初始化(lazy property)

    转自:https://blog.csdn.net/azsx02/article/details/77649527 Python 对象的延迟初始化是指,当它第一次被创建时才进行初始化,或者保存第一次创建 ...

  7. Python(set/list/dict/tuple)

    set集合:set是一个无序,不重复元素的集合.可嵌套列表,字典(可以for循环或者迭代的对象). ######差集: a={11,22} b={22,33} c=a.difference(b) #a ...

  8. Btree索引和Hash索引

    B-Tree 索引 BTree索引是最常用的mysql数据库索引算法,因为它不仅可以被用在=,>,>=,<,<=和between这些比较操作符上,而且还可以用于like操作符, ...

  9. 性能分析(1)- Java 进程导致 CPU 使用率升高,问题怎么定位?

    性能分析小案例系列,可以通过下面链接查看哦 ps:这些分析小案例不能保证百分比正确,是博主学习过程中的总结,仅做参考 前提 本机有一个很占用 CPU 的项目,放在了 Tomcat 下启动着 如何定位 ...

  10. 干货 | 这可能全网最好的BatchNorm详解

    文章来自:公众号[机器学习炼丹术].求关注~ 其实关于BN层,我在之前的文章"梯度爆炸"那一篇中已经涉及到了,但是鉴于面试经历中多次问道这个,这里再做一个更加全面的讲解. Inte ...