Given the sizes of the asteroids in the belt, find the index of the largest asteroid if the pattern can be restored by removing at most one asteroid.
Explanation: Step-by-step: with input [1, 2, 3, 4, 5], we calculate the maximum increasing subsequence ending at each index. The maximum increasing subsequence ending at index 4 is 1, 2, 3, 4, 5. We then remove index 4 and calculate the maximum increasing subsequence of the subsequence without index 4, which is 1, 2, 3, 4, 1, 2, 3. Since the maximum increasing subsequence ending at index 4 is not equal to the maximum increasing subsequence of the subsequence without index 4, we return -1.
Explanation: Step-by-step: with input [1, 2, 3, 4, 1, 2, 3], we calculate the maximum increasing subsequence ending at each index. The maximum increasing subsequence ending at each index is the same. We then remove each index and calculate the maximum increasing subsequence of the subsequence without that index. Since the maximum increasing subsequence ending at each index is the same, we return -1.
Master coding challenges related to Arrays and solve the [Backup] Galaxy Navigator problem optimally.
No dry run loaded.
🚀 Practice this problem
Run code, get AI hints & track streak