algorithm - Distribution strategy to minimize the maximum value in an array -
recently, come across problem in solving problem. have solution myself. i'm not quite satisfied time complexity. so, i'm turning guys possible better solution. problem follows:
suppose there integer array int arr[n]. have integer value val in hand distribute arr. example, can increase arr[0] val, or arr[2] 1 , arr[3] val - 1, , on. goal minimize maximum value in arr after distribution. there may multiple solutions, , do.
my current solution follows. it's trivial , takes o(val) time. have feeling better solution exists.
for (int = 0; < val; ++i) { // increase minimum value in arr 1 }
here's linear-time algorithm. first try increase current max. if have keep going, distribute rest evenly possible. in untested python:
def distribute(arr, val): maxarr = max(arr) x in arr: val -= min(maxarr - x, val) return maxarr - (-val) // len(arr) the double minus silliness ceiling division.
Comments
Post a Comment