• + 0 comments

    Great explanation here: https://rohangupta-3817.medium.com/hackerrank-dp-equal-5adc78771571

    My solution:

    ####################################################
    def get_min_value(arr):
        return min(arr)
    
    
    ####################################################
    def equal(arr):
        min_value = get_min_value(arr)
        totals_table = []
    
        for index in range(0, len(arr)):
            to_compute = arr[index]
            totals_table.append(calculate(to_compute, min_value))
    
        # Tabulate results
        totals_final = []
    
        for outer in range(0,5):
            tally = 0
            for index in range(0, len(totals_table)):
                tally += totals_table[index][outer]
            totals_final.append(tally)
    
        return min(totals_final)
    
    
    ####################################################
    def calculate(element, min_val):
        num_rebase_const = 4
        totals = []
    
        for subtractor in range(0, num_rebase_const + 1):
            baseline = min_val - subtractor
            dist = element - baseline
            totals.append(apply_formula(dist))
    
        return totals
    
    
    ####################################################
    def apply_formula(value):
        return int(value / 5) + int((value % 5) / 2) + int((value % 5) % 2)