Iterate form last to current position and find where the task will happen next .
d = [ 0 ] * ( m + 1 ) NEW_LINE for i in range ( m - 1 , pos , - 1 ) : NEW_LINE INDENT if isRunning [ arr [ i ] ] : NEW_LINE INDENT d [ arr [ i ] ] = i NEW_LINE DEDENT DEDENT