TikTok Interview Question

I got a dynamic programming question.