Page tree
Skip to end of metadata
Go to start of metadata
  1. Minimum Absolute Difference in an Array

    solved
    def minimumAbsoluteDifference(arr):
        arr.sort()
    
    
        min = 1000000000
        
        for i in range(1, len(arr)):
            diff = abs(arr[i] - arr[i-1])
            if diff < min:
                min = diff
        
        return min
  2. Luck Balance

    solved...
    # Complete the luckBalance function below.
    def luckBalance(k, contests):
    
        important_lucks = []
        unimportant_lucks = []
    
        def sumRange(target, start, end):
            result = 0
            for i in range(start, end):
                result += target[i]
            return result
    
        # loop separate important with luck
        for contest in contests:
            luck = contest[0]
            important = contest[1]
    
            if important == 1:
                important_lucks.append(luck)
            else:
                unimportant_lucks.append(luck)
    
        # important_contest sort luck desc
        important_lucks.sort(reverse=True)
    
        luck_balance = 0
        # merge sum luck_balance - first: important loop - sum or minus, second, sum unimportant loop
    
        important_lucks_length = len(important_lucks)
    
        if k >= len(important_lucks):
            luck_balance += sumRange(important_lucks, 0, important_lucks_length)
    
        else:
            luck_balance += sumRange(important_lucks, 0, k)
            luck_balance -= sumRange(important_lucks, k, important_lucks_length)
    
        for luck in unimportant_lucks:
            luck_balance += luck
    
        return luck_balance
  3. Greedy Florist

    solved
    def getMinimumCost(k, c):
    
        c.sort()
    
        sum_price = 0
        
        for i in range(len(c)):
            original_price = c[-1 * (i+1)]
            previous_purchase = math.floor(i / k)
    
            sum_price += (previous_purchase + 1) * original_price
    
        return sum_price



  • No labels