-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum Lights To Activate
78 lines (48 loc) · 1.44 KB
/
Minimum Lights To Activate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Problem Description
There is a corridor in a Jail which is N units long. Given an array A of size N. The ith index of this array is 0 if the light at ith position is faulty otherwise it is 1.
All the lights are of specific power B which if is placed at position X, it can light the corridor from [ X-B+1, X+B-1].
Initially all lights are off.
Return the minimum number of lights to be turned ON to light the whole corridor or -1 if the whole corridor cannot be lighted.
Problem Constraints
1 <= N <= 1000
1 <= B <= 1000
Input Format
First argument is an integer array A where A[i] is either 0 or 1.
Second argument is an integer B.
Output Format
Return the minimum number of lights to be turned ON to light the whole corridor or -1 if the whole corridor cannot be lighted.
Example Input
Input 1:
A = [ 0, 0, 1, 1, 1, 0, 0, 1].
B = 3
Input 2:
A = [ 0, 0, 0, 1, 0].
B = 3
Example Output
Output 1:
2
Output 2:
-1
*/
int Solution::solve(vector<int> &A, int a) {
int count = 0;
int n = A.size();
int i = 0;
while(i < n){
int right = min( i+a-1, n-1);// max coz should not go out of bound
int left = max(i-a+1, 0);
bool bulbFound = false;
while(right >= left){
if(A[right] == 1){
bulbFound = true;
break;
}
right--;
}
if(!bulbFound) return -1;
count++;
i = right + a;
}
return count;
}