简体   繁体   English

生成递归以找到具有最大总和的子列表

[英]Generative recursion to find the sublist with the maximum sum

I am trying to solve a generative recursion problem in Python. 我正在尝试解决Python中的生成递归问题。 The question is: 问题是:

  • In a list that consists of integers, find the adjoining sublist which has the largest sum and return that sum. 在由整数组成的列表中,找到具有最大和的相邻子列表,然后返回该和。
  • For example, if the given list is [−2, 1, −3, 4, −1, 2, 1, −5, 4], the adjoining sublist which has the largest sum is [4, −1, 2, 1], which has a sum 6 例如,如果给定列表为[-2,1,-3,4,-1,2,1,1,-5,4],则具有最大和的相邻子列表为[4,-1,2,1 ],总和为6

I have to follow the given algorithm to solve find_max: 我必须遵循给定的算法来解决find_max:

  1. Split given list (at the midpoint) into two: L_left and L_right. 将给定列表(中点)分为两个:L_left和L_right。
  2. Return the maximum value of following 3: 返回以下3的最大值:
    • The maximum sum of any sublist resides entirely in L_left (using a recursive call to find_max). 任何子列表的最大和都完全位于L_left中(使用对find_max的递归调用)。
    • The maximum sum of any sublist resides entirely in L_right (using a recursive call to find_max). 任何子列表的最大和都完全位于L_right中(使用对find_max的递归调用)。
    • The maximum sublist that overlaps L_left and L_right; 与L_left和L_right重叠的最大子列表; ie,
      • First: Find max sum of any sublist starting from the midpoint (towards the left) and ending at some point on the left of the midpoint 第一:查找从中点(向左)到中点左侧某点结束的任何子列表的最大和
      • Second: Find the max sum of any sublist starting from the midpoint (towards the right) and ending at some point on the right of the midpoint 第二:找到从中点(向右)到中点右边的某个点结束的任何子列表的最大和
      • Finally: Add the two max sums. 最后:将两个最大和相加。

I have tried the following: 我尝试了以下方法:

def find_max(L):
    length = len(L)
    mid_index = length/2
    if length == 1:
        return L[0]
    else:
        left = find_max(L[0:(length/2)])
        right = find_max(L[(length/2):length])
        max_subset = max(left,right,left+right)
        return max_subset

This is able to solve for lists with length 2. How do I extend this to work for a list with more elements? 这样就可以解决长度为2的列表。如何将其扩展为适用于具有更多元素的列表?

You didn't consider following: 您没有考虑以下事项:

  • another base case: L is [] 另一个基本情况:L是[]
  • left half and right half should be consecutive. 左半部分和右半部分应连续。
    • According to your code, if L is [2, -5, 3] , in the first recursion, left + right will yield 5. 根据您的代码,如果L[2, -5, 3] ,则在第一次递归中, left + right将产生5。

def find_max(L):
    length = len(L)
    mid_index = length/2
    if length == 0:
        return 0
    elif length == 1:
        return max(L[0], 0)

    left = find_max(L[:mid_index])
    right = find_max(L[mid_index:])

    left_half = right_half = 0
    # to the left
    accum = 0
    for x in L[mid_index-1::-1]:
        accum += x
        left_half = max(left_half, accum)

    # to the right
    accum = 0
    for x in L[mid_index:]:
        accum += x
        right_half = max(right_half, accum)

    return max(left, right, left_half + right_half)


assert find_max([]) == 0
assert find_max([-1]) == 0
assert find_max([1, 2, 3]) == 6
assert find_max([2, -5, 3]) == 3
assert find_max([-5, 1, 4, -2, 2, -1, 2, -3, 1, -3, 4]) == 6

Without for loop: 没有for循环:

def sum_max(L, accum=0, max_value=0):
    if not L:
        return max_value
    accum += L[0]
    return sum_max(L[1:], accum, max(max_value, accum))

def find_max(L):
    ...
    left_half = sum_max(L[mid_index-1::-1])
    right_half = sum_max(L[mid_index:])
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM