Minimum time to finish tasks without skipping two consecutive | arr [ ] represents time taken by n given tasks ; Corner Cases ; Initialize value for the case when there is only one task in task list . First task is included ; 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
def minTime ( arr , n ) : NEW_LINE INDENT if ( n <= 0 ) : return 0 NEW_LINE DEDENT incl = arr [ 0 ] NEW_LINE excl = 0 NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT incl_new = arr [ i ] + min ( excl , incl ) NEW_LINE excl_new = incl NEW_LINE incl = incl_new NEW_LINE excl = excl_new NEW_LINE DEDENT return min ( incl , excl ) NEW_LINE DEDENT arr1 = [ 10 , 5 , 2 , 7 , 10 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( minTime ( arr1 , n1 ) ) NEW_LINE arr2 = [ 10 , 5 , 7 , 10 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE print ( minTime ( arr2 , n2 ) ) NEW_LINE arr3 = [ 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 ] NEW_LINE n3 = len ( arr3 ) NEW_LINE print ( minTime ( arr3 , n3 ) ) NEW_LINE