Python:最大子阵列总和

时间:2017-07-22 02:52:18

标签: python sum logic maximize sub-array

我今天刚尝试开始学习Python,所以当涉及到它的功能时,我并不知情。但是,我找到了最大的子阵列问题,并希望尝试使用我所拥有的一些简单的逻辑命令来解决它。我已经卡住了,几乎可以肯定问题出在我的逻辑上而不是语法上,尽管我可能错了。到目前为止,这是我的代码......

def maxSequence(arr): #Latest attempt, some issue.
  old_arr = []
  print(arr)
  while old_arr != arr:
    old_arr = arr
    if arr[0] > 0 and arr[len(arr)-1] > 0: #For when both sides are positive, need to be sure there is not anything to be gained by eliminating some side section
      new_sum = 0
      y=0
      while new_sum >= 0 and y != -1:
        new_sum = sum(arr[:y])
        y=y+1
        if y == len(arr)-1:
          y=-1
      if y != -1:
        arr = arr[y-1:]
      print("left %s" %(new_sum))
      print("left %s" % (arr))
      new_sum = 0
      y = 0
      while new_sum >= 0 and y != -1:
        new_sum=sum(arr[(len(arr)-y):])
        y=y+1
        if y == len(arr)-1:
          y=-1
      if y != -1:
        arr = arr[:len(arr)-y+1]
      print("right %s" %(new_sum))
      print("right %s" % (arr))
    else:
      while arr[0] < 0 or arr[len(arr)-1] < 0: #To eliminate negatives on sides
        if arr[0] < 0:
         arr = arr[1:]
        if arr[len(arr)-1] < 0:
         arr = arr[:len(arr)-1]
        print("negative %s" % (arr))
  print(arr)
  print(sum(arr))

各种打印功能显示程序做出的每个决定,并帮助我可视化执行循环时发生的事情。

给出列表

时,没有给出正确的105结果
[26, -25, -23, -2, 3, -6, -5, 15, 7, 8, 17, 15, 29, -2, 16, -25, -8, -25, -27, 15, -29, -11, -12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, -18, 6, -13, -13, 25, -22, 8, 9, -4, -25, 17, -26]

最终总计为94,将列表缩小为

[21, 20, 30, -29, 17, 9, -19, 28, 11, 6]

对不起,很长的帖子,但我无法弄清楚我做错了什么。感谢您的帮助!

这是上述列表作为输入给出的输出,我已经完成并查看了每个步骤,列表中的每个消除对我来说都是合乎逻辑的,我不知道如何最终得到一个结束总和105.如果有人能帮我理解,我真的很感激!

[26, -25, -23, -2, 3, -6, -5, 15, 7, 8, 17, 15, 29, -2, 16, -25, -8, -25, 
-27, 15, -29, -11, -12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, 
-25, 27, -18, 6, -13, -13, 25, -22, 8, 9, -4, -25, 17, -26]
negative [26, -25, -23, -2, 3, -6, -5, 15, 7, 8, 17, 15, 29, -2, 16, -25, 
-8, -25, -27, 15, -29, -11, -12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 
11, 6, -10, -25, 27, -18, 6, -13, -13, 25, -22, 8, 9, -4, -25, 17]
left -22
left [-2, 3, -6, -5, 15, 7, 8, 17, 15, 29, -2, 16, -25, -8, -25, -27, 15, 
-29, -11, -12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, 
-18, 6, -13, -13, 25, -22, 8, 9, -4, -25, 17]
right -8
right [-2, 3, -6, -5, 15, 7, 8, 17, 15, 29, -2, 16, -25, -8, -25, -27, 15, 
-29, -11, -12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, 
-18, 6, -13, -13, 25, -22, 8, 9, -4]
negative [3, -6, -5, 15, 7, 8, 17, 15, 29, -2, 16, -25, -8, -25, -27, 15, 
-29, -11, -12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, 
-18, 6, -13, -13, 25, -22, 8, 9]
left -3
left [-5, 15, 7, 8, 17, 15, 29, -2, 16, -25, -8, -25, -27, 15, -29, -11, 
-12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, -18, 6, 
-13, -13, 25, -22, 8, 9]
right -5
right [-5, 15, 7, 8, 17, 15, 29, -2, 16, -25, -8, -25, -27, 15, -29, -11, 
-12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, -18, 6, 
-13, -13, 25]
negative [15, 7, 8, 17, 15, 29, -2, 16, -25, -8, -25, -27, 15, -29, -11, 
-12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, -18, 6, 
-13, -13, 25]
left -5
left [-12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, 
-18, 6, -13, -13, 25]
right -1
right [-12, 1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, 
-18, 6]
negative [1, -14, 21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, -18, 
6]
left -13
left [21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27, -18, 6]
right -12
right [21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27]
left 84
left [21, 20, 30, -29, 17, 9, -19, 28, 11, 6, -10, -25, 27]
right -8
right [21, 20, 30, -29, 17, 9, -19, 28, 11, 6]
left 77
left [21, 20, 30, -29, 17, 9, -19, 28, 11, 6]
right 53
right [21, 20, 30, -29, 17, 9, -19, 28, 11, 6]
[21, 20, 30, -29, 17, 9, -19, 28, 11, 6]
94

4 个答案:

答案 0 :(得分:2)

我用一个小例子来说明你的算法有什么问题。

说输入是

[2, -3, 1]

[2]显然是最大的子阵列。但是,您的算法将查看此部分:

[2, -3, 1]
 ^^^^^

并看到它可以通过删除[2, -3]来增加数组的总和。

如果最大子阵列是负子阵列的一部分,则从末尾删除负子阵列是错误的。您需要一个更智能的算法。

答案 1 :(得分:0)

为什么不使用 Kadane的算法,这需要花费O(n)时间??

Kadane的算法(DP):

Initialize:
    max_so_far = 0
    max_ending_here = 0

Loop for each element of the array
  (a) max_ending_here = max_ending_here + a[i]
  (b) if(max_ending_here < 0)
            max_ending_here = 0
  (c) if(max_so_far < max_ending_here)
            max_so_far = max_ending_here
return max_so_far

答案 2 :(得分:0)

def contiguous(list_val, size):
    if size<=len(list_val):
        iterations = len(list_val)-size+1
        count = 0
        num = []
        for i in range(iterations):
            sum_val = 0
            for j in range(count,size):
            sum_val+=list_val[j]
            num.append(sum_val)
            count +=1
            size +=1
        print(max(num))
    else:
        print("Error: given size {} is greater than length of list".format(size))

    list_val = input("Enter list: ")
    size = input("Enter the pos: ")
    contiguous(list_val, size)

答案 3 :(得分:0)

试试这个代码。这对 O(n) 时间复杂度有效,因为它只运行了 n 次。

def manSubArraySum(a):
    currentbest = 0
    overallbest = a[0]
    for p in a: # [1,2,-2,4]
        currentbest = currentbest+p # first step cb = -10
        if currentbest> overallbest: # !true
            overallbest = currentbest
        if currentbest<0: # nottrue
            currentbest =0 # 0

    return overallbest