Solutions for arrange wooden sticks

    • @DarrenNop
    • @william, submitted in Python 2, achived 100% score
    def mergesort2(A):
        global count
        if len(A) > 1:
            n = len(A)
            m = int(n/2)
            L = A[:m]
            R = A[m:n]
    
            mergesort2(L)
            mergesort2(R)
    
            i = 0
            j = 0
            k = 0
            n1 = m
            n2 = n-m
    
            while i < n1 and j < n2:
                if L[i] <= R[j]:
                    A[k] = L[i]
                    i += 1
                    k += 1
                else:
                    A[k] = R[j]
                    j += 1
                    k += 1
                    count += n1-i
    
            while i < n1:
                A[k] = L[i]
                i+=1
                k+=1 
            while j < n2:
                A[k] = R[j]
                j+=1
                k+=1
    
    
    # Complete the solve function below.
    def solve(arr):
        global count
        count = 0
        mergesort2(arr)
        return count
    
    
    • @DarrenNop
    • @william, submitted in Python 3, achived 100% score
    def mergesort2(A):
        global count
        if len(A) > 1:
            n = len(A)
            m = int(n/2)
            L = A[:m]
            R = A[m:n]
    
            mergesort2(L)
            mergesort2(R)
    
            i = 0
            j = 0
            k = 0
            n1 = m
            n2 = n-m
    
            while i < n1 and j < n2:
                if L[i] <= R[j]:
                    A[k] = L[i]
                    i += 1
                    k += 1
                else:
                    A[k] = R[j]
                    j += 1
                    k += 1
                    count += n1-i
    
            while i < n1:
                A[k] = L[i]
                i+=1
                k+=1 
            while j < n2:
                A[k] = R[j]
                j+=1
                k+=1
    
    
    # Complete the solve function below.
    def solve(arr):
        global count
        count = 0
        mergesort2(arr)
        return count