1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
''' 时间复杂度的渐进表示法: T(n) = O(f(n)) 表示时间复杂度有一个上界。T(n) <= Cf(n)
T(n) = Ω(g(n)) 表示时间复杂度有一个上界。T(n) >= Ωg(n)
T(n) = Θ(g(n)) 表示同时有一个上界和下界。
'''
aa = [1,2,-1,8,-1]
def MaxsubseqSum(a,N): MaxSum = 0 for i in range(0,N): for j in range(i,N): ThisSum = 0 for k in range(i,j+1): ThisSum += a[k] if (ThisSum>MaxSum): MaxSum = ThisSum return MaxSum
a_maxsubseqsum = MaxsubseqSum(aa,len(aa)) print(a_maxsubseqsum) print(' !!!!!!! ')
aa = [1,2,3,4,-11,2] def MaxsubseqSum(a,N): MaxSum = 0 for i in range(0,N): ThisSum = 0 for j in range(i,N): ThisSum += a[j] if (ThisSum>MaxSum): MaxSum = ThisSum return MaxSum
a_maxsubseqsum = MaxsubseqSum(aa,len(aa)) print(a_maxsubseqsum)
''' 但是这里必须提一下: 二分算法是分治算法的特殊情况! 区别在于: 二分算法是一次比较,直接扔掉不符合要求的另一半, 分治算法则只是作了划分,并没有缩减问题的规模。
二分法和分而治之法的 时间复杂度推导在py文件不方便看,转《二分法、分而治之时间复杂度推导.pages》。 '''
aa = [1,2,3,4,-11,2] def MaxSubseqSumnew(a,N): ThisSum = 0 MaxSum = 0 for i in range(N): ThisSum += a[i] if ThisSum > MaxSum: MaxSum = ThisSum if ThisSum < 0: ThisSum = 0 return MaxSum fastest = MaxSubseqSumnew(aa,len(aa)) print(fastest)
|