Google interview question

Focus on two pointers, graphs, and dp