【问题标题】:Question About Kadane's Algorithm of Finding Continuous Subarray with Maximum Sum关于 Kadane 求最大和连续子数组的算法的问题
【发布时间】:2019-11-11 13:54:13
【问题描述】:

我检查了用于使用 Kadane 算法找到具有最大和的连续子数组的解决方案,我不知道为什么我们需要在代码中使用全局最大值(以下代码中的 global_max)。

我认为在迭代整个数组后返回局部最大值(以下代码中的 current_max 变量)应该足够了 哪位专家可以提供一些建议吗?真的很感激!

以下是python代码用于查找最大和的连续子数组

def find_array(nums):
    current_max = nums[0]
    #I think it should be fine if remove global_max
    global_max = nums[0]
    for i in range(1,len(nums)):

        current_max = max(nums[i],current_max+nums[i])
        if current_max > global_max:
            global_max = current_max
    return global_max

【问题讨论】:

    标签: algorithm


    【解决方案1】:

    考虑案例1 -10

    本地最大值将为-9。全局将是1

    【讨论】:

    • 非常感谢!真的很感激:)
    • 对不起,我是stackoverflow的新手,我投票并标记为正确,如果我做得不对,请随时告诉我:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 1970-01-01
    • 2021-11-09
    • 2019-10-13
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多