Page tree
Skip to end of metadata
Go to start of metadata
minimumAbsoluteDifference
def minimumAbsoluteDifference(arr):
    sorted_list = sorted(arr)
    min_val = sorted_list[-1]

    for i in range(len(arr) - 1):
        cur = abs(sorted_list[i] - sorted_list[i + 1])
        if cur < min_val:
            min_val = cur

    return min_val
Luck Balance
def luckBalance(k, contests):
    sorted_contests = sorted(contests, key=lambda x: (x[1], -x[0]))
    total_sum = 0
    for i in sorted_contests:
        if i[1] == 0:
            total_sum += i[0]
        elif k > 0:
            total_sum += i[0]
            k -= 1
        else:
            total_sum -= i[0]

    return total_sum
Greedy Florist
def getMinimumCost(k, c):
    sorted_c = sorted(c, reverse=True)

    sum = 0
    for i, val in enumerate(sorted_c):
        mul = i // k
        sum += (mul + 1) * sorted_c[i]

    return sum
  • No labels