Google Phone Screen
Anonymous User
12146

We have a street of length n, and we wish to light up the entire street (from 0 to n) with streetlights. We have streetlights at locations along the street, and for each streetlight we know it's position on the street and how far the light can shine to the left and to the right. Find the minimum number of streetlights needed to light up the whole street.

For example, if n = 10, and streetlights = [[0, 5], [1, 3], [5, 4], [8, 3]], the answer is 2, since all we need are streetlights[0] and streetlights[3] to light up the entire street from 0 to 10. Another example is n = 8, streetlights = [[2, 3], [8, 2]], this would return -1 since the first streetlight lights up [0, 5] and the second lights up [6,8], and there's a gap between 5-6 that isn't lit.

I tried doing 2D dp but couldn't get the transtion function. Thoughts?

Comments (13)