Minimum time to finish tasks without skipping two consecutive | arr [ ] represents time taken by n given tasks ; Corner Cases ; First task is exluded ; Process remaining n - 1 tasks ; Time taken if current task is included There are two possibilities ( a ) Previous task is also included ( b ) Previous task is not included ; Time taken when current task is not included . There is only one possibility that previous task is also included . ; Update incl and excl for next iteration ; Return maximum of two values for last task ; Driver code
< ? php function minTime ( $ arr , $ n ) { if ( $ n <= 0 ) return 0 ; $ excl = 0 ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { $ incl_new = $ arr [ $ i ] + min ( $ excl , $ incl ) ; $ excl_new = $ incl ; $ incl = $ incl_new ; $ excl = $ excl_new ; } return min ( $ incl , $ excl ) ; } $ arr1 = array ( 10 , 5 , 2 , 7 , 10 ) ; $ n1 = sizeof ( $ arr1 ) ; echo minTime ( $ arr1 , $ n1 ) , " STRNEWLINE " ; $ arr2 = array ( 10 , 5 , 7 , 10 ) ; $ n2 = sizeof ( $ arr2 ) ; echo minTime ( $ arr2 , $ n2 ) , " STRNEWLINE " ; $ arr3 = array ( 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 ) ; $ n3 = sizeof ( $ arr3 ) ; echo minTime ( $ arr3 , $ n3 ) ; ? >