Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
How can you calculate the longest ideal subsequence that ends at a specific index i?
Can you calculate it for all positions i? How can you use previously calculated answers to calculate the answer for the next position?