You are given an integer n and an array ranges of length n + 1. The segment [0, n] on the number line must be covered. There are n + 1 points at positions 0, 1, ..., n. Activating point i covers the interval [i - ranges[i], i + ranges[i]].
Return the minimum number of points to activate so that the whole segment [0, n] is covered, or -1 if that is impossible.
Examples
Example 1
Input: n = 5, ranges = [3, 4, 1, 1, 0, 0]
Output: 1
Explanation: Point 1 covers [1-4, 1+4] = [-3, 5], which contains [0, 5].
Example 2
Input: n = 3, ranges = [0, 0, 0, 0]
Output: -1
Explanation: No point covers anything beyond itself.
Constraints
1≤n≤104
ranges.length=n+1
0≤ranges[i]≤100
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up