Find minimum speed to finish all Jobs | Function to check if the person can do all jobs in H hours with speed K ; Function to return the minimum speed of person to complete all jobs ; If H < N it is not possible to complete all jobs as person can not move from one element to another during current hour ; Max element of array ; Use binary search to find smallest K ; Driver program ; Print required maxLenwer
function isPossible ( A , n , H , K ) { var time = 0 ; for ( var i = 0 ; i < n ; ++ i ) time += parseInt ( ( A [ i ] - 1 ) / K ) + 1 ; return time <= H ; } function minJobSpeed ( A , n , H ) { if ( H < n ) return - 1 ; var max = A . reduce ( ( a , b ) => Math . max ( a , b ) ) ; var lo = 1 , hi = max ; while ( lo < hi ) { var mi = lo + parseInt ( ( hi - lo ) / 2 ) ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } var A = [ 3 , 6 , 7 , 11 ] , H = 8 ; var n = A . length ; document . write ( minJobSpeed ( A , n , H ) ) ;