Minimum time required to schedule K processes | Function to find minimum required time to schedule all process ; Stores max element from A [ ] ; Find the maximum element ; Stores frequency of each element ; Stores minimum time required to schedule all process ; Count frequencies of elements ; Find the minimum time ; Decrease the value of K ; Increment tmp [ i / 2 ] ; Increment the count ; Return count , if all process are scheduled ; Increment count ; Return the count ; If it is not possible to schedule all process ; Driver code
def minTime ( A , n , K ) : NEW_LINE INDENT max_ability = A [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT max_ability = max ( max_ability , A [ i ] ) NEW_LINE DEDENT tmp = [ 0 for i in range ( max_ability + 1 ) ] NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT tmp [ A [ i ] ] += 1 NEW_LINE DEDENT i = max_ability NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( tmp [ i ] != 0 ) : NEW_LINE INDENT if ( tmp [ i ] * i < K ) : NEW_LINE INDENT K -= ( i * tmp [ i ] ) NEW_LINE tmp [ i // 2 ] += tmp [ i ] NEW_LINE count += tmp [ i ] NEW_LINE if ( K <= 0 ) : NEW_LINE INDENT return count NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( K % i != 0 ) : NEW_LINE INDENT count += ( K // i ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += ( K // i ) NEW_LINE DEDENT return count NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 1 , 7 , 2 , 4 ] NEW_LINE N = 5 NEW_LINE K = 15 NEW_LINE print ( minTime ( arr , N , K ) ) NEW_LINE DEDENT