Please explain last part

what are the distict case why sir added dp array in the end

Hi,

The answer is to stored in dp array so assignment is done at end. Can you please elaborate your doubt.