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


    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/9697944.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

    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: [-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.


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

    示例:

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

    进阶:

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


    动态规划法:设sum[i]为以第i个元素结尾且和最大的连续子数组。假设对于元素i,所有以它前面的元素结尾的子数组的长度都已经求得,那么以第i个元素结尾且和最大的连续子数组实际上,要么是以第i-1个元素结尾且和最大的连续子数组加上这个元素,要么是只包含第i个元素,即sum[i] = max(sum[i-1] + a[i], a[i])。可以通过判断sum[i-1] + a[i]是否大于a[i]来做选择,而这实际上等价于判断sum[i-1]是否大于0。由于每次运算只需要前一次的结果,因此并不需要像普通的动态规划那样保留之前所有的计算结果,只需要保留上一次的即可,因此算法的时间和空间复杂度都很小

    12ms:

     1 class Solution {
     2     func maxSubArray(_ nums: [Int]) -> Int {
     3         //动态规划法
     4         var sum:Int = nums[0]
     5         var n = nums[0]
     6         for i in 1..<nums.count
     7         {
     8             if n>0
     9             {
    10                 n+=nums[i]
    11             }
    12             else
    13             {
    14                 n = nums[i]
    15             }
    16             if sum<n
    17             {
    18                 sum = n
    19             }         
    20         }
    21         return sum   
    22     }
    23 }

    16ms:

     1 class Solution {
     2     func maxSubArray(_ nums: [Int]) -> Int {
     3         guard nums.count > 0 else {
     4             return 0
     5         }
     6         
     7         var result = Int(-INT32_MAX - 1)
     8         var sum = 0
     9         for num in nums {
    10             sum += num
    11             result = max(result, sum)
    12             if sum < 0 {
    13                 sum = 0
    14             }
    15         }
    16         
    17         
    18         return result
    19     }
    20 }

    扫描法:出自《编程珠机》

     1 class Solution {
     2     func maxSubArray(_ nums: [Int]) -> Int {
     3         //扫描法
     4         var current:Int = nums[0]
     5         var sum = nums[0]
     6         //考虑如果全是负数,那么返回最大的负数,
     7         //如果最后的和为正,那么就使用扫描法
     8          for i in 1..<nums.count
     9         {
    10             //当前数小于0则舍去,
    11             //否则将会影响接下来的和
    12             //继续下一个数
    13             if current<0
    14             {
    15                 current = nums[i]
    16             }
    17             else
    18             {
    19                 //如果当前数不小于0,那么他会对接下来的和有积极影响
    20                 current+=nums[i]
    21             }
    22             //这里既实现了负数返回最大也实现了扫描法
    23             if current>sum
    24             {
    25                 sum = current
    26             }
    27             //这里其实已经隐式的列举了所有可能,保留了所有可能的最大值
    28         }
    29         return sum
    30     }
    31 }
  • 相关阅读:
    [每日一题]石子合并 -- 区间DP
    [每日一题]: 最长上升子序列 AND 最长不上升子序列
    [每日一题]:最大子矩阵和
    入门DP--最大子段和
    [转载]:正确的提问方式
    springmvc.xml
    service层springservice.xml文件
    aop切面配置
    配置事务通知
    短信验证
  • 原文地址:https://www.cnblogs.com/strengthen/p/9697944.html
Copyright © 2020-2023  润新知