The Telecom Dilemma

0

0 votes
Dynamic Programming, Greedy Algorithms
Problem

Gourav, our friend wants to set up his telecom company that provides free 5G data to everyone in the city. He started working on his company and saw that the cost of buying new cell towers was very high. He went around the city to find the cell towers that he could rent for his company. 

Gourav had to cover the total area of the city. He saw that there were multiple towers in the area in a straight line that can provide signals to everyone within their range of operation. Jeet wanted to launch his company at the earliest and asked for your help.

The city starts at point 0 and ends at the point N. There are N+1 towers located at points [0,1,2,.......N] in the city at a distance of 1 unit from the previous point. Each tower X located at point P has a range capacity R that can provide high speed signals to the range[PR,P+R]. Since, the company is new and has limited funding, you need to find the minimum number of towers Jeet needs to rent, in order to cover the entire city with his high speed 5G network. If there is even 1 spot that cannot be covered, return 1.

 

Input Format:

The 1st line contains N, the end point

The next line contains the array ranges of size N+1, where ith element has signal range ranges[i].

Output Format:

The minimum number of towers to rent, if the entire range cannot be covered, then print -1

Constraints :

1N104

ranges.length==N+1

0range[i]100

 

Sample Input
5
3 4 1 1 0 0
Sample Output
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
The tower at point 0 can cover the range [-3,3]
The tower at point 1 can cover the range [-3,5]
The tower at point 2 can cover the range [1,3]
The tower at point 3 can cover the range [2,4]
The tower at point 4 can cover the range [4,4]
The tower at point 5 can cover the range [5,5]
Renting the second tower at point 1 will ensure connectivity in the entire range [0,5]
Editor Image

?