Assume the journey starts at time 0.
If the time taken to get to the train station exceeds Y, then he misses the train.
Otherwise, he has to wait for the train to arrive, which happens at time Y.
So in my solution, max(trainLeaveTime, timeWalkToTrain) will always equal trainLeaveTime (= Y), I think, so I should probably just replace it with trainLeaveTime