A Linear-Time and Space Algorithm for Optimal Traffic Signal