TikTok interview question

Medium Hackerrank question, dynamic programming.