A Linear Time Algorithm Computing the Optimal Speeds Minimizing Energy Under Real-Time Constraints