Basketball tournament

  • + 0 comments

    Hello, the below is my solution : I get error on this line : temp_li.append(list(itertools.combinations(varying_range, r=i))) Memory Error. Please help me in resolving the same.

    ''' https://www.hackerrank.com/contests/hourrank-31/challenges/basketball-tournament-1/problem '''

    import itertools
    from functools import reduce
    n, m = list(map(int, input().split()))
    heights = list(map(int, input().split()))
    heights = sorted(heights)
    
    def list_of_l_r(l, r):
        temp_li = []
        temp_li.clear()
        varying_range = list(range(l,r+1))
        len_ = len(varying_range)
        for i in range(1, len_+1):
            temp_li.append(list(itertools.combinations(varying_range, r=i)))
            print(len(temp_li))
            # yield list(itertools.combinations(varying_range, r=i))
            # li1 = list(itertools.combinations(varying_range, r=i))
    
        print(temp_li)
        return temp_li
    
    def f(i, j):
        return i+j
    def calculate_power(collection):
        # retrieve the heights from the index
        # index comes from collections
        selected_height = []
        for i in collection:
            selected_height.append(heights[i-1])
        # calculate power for them
        combinations = list(itertools.product(selected_height, repeat=2))
        # print(combinations)
    
        power = 0
        for items in combinations:
            power += sum(items)
        # print(power)
        return power
    
    def returnMin(matrix_of_indexes, x):
        height_max = []
        for matrix_items in matrix_of_indexes:
            for list_items in matrix_items:
                # print(list_items)
                power = calculate_power(list_items)
                if power >= x:
                    # check for max value of height
    
                    for i in list_items :
                        height_max.append(heights[i-1])
                    # print("final_heights", height_max)
                    # print("minimum is :")
                    # print(height_max)
                    # print("before returning height_max:", list_items)
                    return max(height_max);
        if height_max == []:
            return -1
    for i in range(m):
        l, r, x = list(map(int, input().split()))
        # print(l, r, x)
    
        # reproduce the sequences
        matrix_of_indexes = list_of_l_r(l, r)
        print(returnMin(matrix_of_indexes, x))