Find minimum time to finish all jobs with given constraints | Utility function to get maximum element in job [ 0. . n - 1 ] ; Returns true if it is possible to finish jobs [ ] within given time 'time ; cnt is count of current assignees required for jobs ; time assigned to current assignee ; If time assigned to current assignee exceeds max , increment count of assignees . ; Else add time of job to current time and move to next job . ; Returns true if count is smaller than k ; Returns minimum time required to finish given array of jobs k -- > number of assignees T -- > Time required by every assignee to finish 1 unit m -- > Number of jobs ; Set start and end for binary search end provides an upper limit on time ; Find the job that takes maximum time ; Do binary search for minimum feasible time ; If it is possible to finish jobs in mid time ; ans = min ( ans , mid ) Update answer ; Driver program
def getMax ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] > result : NEW_LINE INDENT result = arr [ i ] NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT ' NEW_LINE def isPossible ( time , K , job , n ) : NEW_LINE INDENT cnt = 1 NEW_LINE curr_time = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT if curr_time + job [ i ] > time : NEW_LINE INDENT curr_time = 0 NEW_LINE cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curr_time += job [ i ] NEW_LINE i += 1 NEW_LINE DEDENT DEDENT return cnt <= K NEW_LINE DEDENT def findMinTime ( K , T , job , n ) : NEW_LINE INDENT end = 0 NEW_LINE start = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT end += job [ i ] NEW_LINE DEDENT job_max = getMax ( job , n ) NEW_LINE while start <= end : NEW_LINE INDENT mid = int ( ( start + end ) / 2 ) NEW_LINE if mid >= job_max and isPossible ( mid , K , job , n ) : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return ans * T NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT job = [ 10 , 7 , 8 , 12 , 6 , 8 ] NEW_LINE n = len ( job ) NEW_LINE k = 4 NEW_LINE T = 5 NEW_LINE print ( findMinTime ( k , T , job , n ) ) NEW_LINE DEDENT