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

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -