Complexity of Time from O (n^3) to O (n^2) optimization

Source: Internet
Author: User
Title Description

A given array of n integers a[0],a[1],a[2],a[3],.... a[n-1]. You want to output a two-dimensional array of n*n B,
where array b[i,j] (I first method of thinking

For i =0,1,2,... n-2
    for j = i+1,i+2,.... n-1
        add a[i]->a[j] and assign to B[i,j]

Because the j>i is satisfied, the diagonal of the two-dimensional matrix does not need to be computed, and the last line is always j>=i and does not have to be computed, so the first layer for loop is [0~n-2]
The outer loop operand N level, the inner layer is summed with Gaussian n^2 level, so time complexity is O (n^3) The second solution

For i = 0,1,2...n-2
    b[i][i+1] = A[i] + a[i+1] for

i = 0,1,2,3...n-2 for
    j = i+2,i+3,.... n-1
        b[i][j] = b[i] [j] + A[j]

Ideas:
B[0][1] = a[0] + a[1]
B[0][2] = a[0] + a[1] + a[2] = b[0][1] + a[2]
B[0][3] = a[0] + a[1] + a[2] + a[3] = b[0][2] + a[3]
。。。

B-matrix B[i][j] = B[i][j-1] + a[j], so that the intermediate calculation results can be used, but also reduce the repetition of the calculation.
Parsing algorithm, first for loop O (n), second two for Loop O (n^2), so the time complexity of the entire algorithm is O (n^2) program code

"" "
given by n integers a[1],a[2],a[3],.... A[n] Array A. You want to output a two-dimensional array of n*n B,
where the array B[i,j] (I<J) contains an array of items a[i]~a[j] and, that is, and a[i]+a[i+1] ..... A[J] (as long as i>=j, the
value of the item b[i,j] of the array is not specified, so regardless of the output of these values.)
"" " def fun (a):
    n = Len (a)
    B = [[0 for I in range (0,n)] for J in Range (0,n)] for
    I in range (0,n-1): for
        J in R Ange (i+1,n):
            sum = (A[i]+a[j]) * (j-i+1)//2
            b[i][j] = sum
    return B

def fun2 (a):
    n = Len (a)
    B = [[0 for I in range (0,n)] for J in Range (0,n)] for
    I in range (0,n-1):
        b[i][i+1] = A[i] + a[i+1] for

    i in Range (0,n-1):
        for J in Range (I+2,n):
            b[i][j] = b[i][j-1] + a[j]
    return B


def test ():
    A = [A. 4,5]
    result = Fun (a) for
    I in range (len (A)):
        print (Result[i])
def test2 ():
    A = [1,2,3,4,5]
    result = Fun2 (a) for
    I in range (Len (a)):
        print (result[i])

if __name__  = = ' __main__ ':
    Test ()
    print ("=====")
    test2 ()

The program needs to be verified, if there are errors thank you for pointing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.