N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times.
Note: 0 represents the bulb is off and 1 represents the bulb is on.
Simulate the process exactly as described: for each bulb, if it is off, press its switch, which toggles it and all bulbs to its right. This brute-force approach checks every bulb and updates the state array each time.
classSolution {
public:int bulbs(vector<int>& A) {
int n = A.size(), ans =0;
vector<int> state = A;
for (int i =0; i < n; ++i) {
if (state[i] ==0) {
++ans;
for (int j = i; j < n; ++j) state[j] =1- state[j];
}
}
return ans;
}
};
publicclassSolution {
publicintbulbs(int[] A) {
int n = A.length, ans = 0;
int[] state = A.clone();
for (int i = 0; i < n; i++) {
if (state[i]== 0) {
ans++;
for (int j = i; j < n; j++) {
state[j]= 1 - state[j];
}
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funbulbs(A: IntArray): Int {
val n = A.size
var ans = 0val state = A.copyOf()
for (i in0 until n) {
if (state[i] ==0) {
ans++for (j in i until n) state[j] = 1 - state[j]
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defbulbs(self, A: list[int]) -> int:
n = len(A)
ans =0 state = A[:]
for i in range(n):
if state[i] ==0:
ans +=1for j in range(i, n):
state[j] =1- state[j]
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnbulbs(A: Vec<i32>) -> i32 {
let n = A.len();
letmut ans =0;
letmut state = A.clone();
for i in0..n {
if state[i] ==0 {
ans +=1;
for j in i..n {
state[j] =1- state[j];
}
}
}
ans
}
}
Instead of simulating every flip, we can track the number of flips so far. Each flip inverts the meaning of the bulbs to the right. If the current bulb’s state, after accounting for the number of flips so far, is off, we need to flip at this position. This way, we only count the minimum necessary flips.
classSolution {
public:int bulbs(vector<int>& A) {
int ans =0, flip =0, n = A.size();
for (int i =0; i < n; ++i) {
if ((A[i] ^ (flip &1)) ==0) {
++ans;
++flip;
}
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
funcbulbs(A []int) int {
n:= len(A)
ans, flip:=0, 0fori:=0; i < n; i++ {
if (A[i]^flip)&1==0 {
ans++flip++ }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
publicclassSolution {
publicintbulbs(int[] A) {
int ans = 0, flip = 0, n = A.length;
for (int i = 0; i < n; i++) {
if ((A[i]^ (flip & 1)) == 0) {
ans++;
flip++;
}
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
funbulbs(A: IntArray): Int {
var ans = 0var flip = 0for (i inA.indices) {
if ((A[i] xor (flip and 1)) ==0) {
ans++ flip++ }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defbulbs(self, A: list[int]) -> int:
ans =0 flip =0for a in A:
if (a ^ (flip &1)) ==0:
ans +=1 flip +=1return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
impl Solution {
pubfnbulbs(A: Vec<i32>) -> i32 {
letmut ans =0;
letmut flip =0;
for&a in&A {
if (a ^ (flip &1)) ==0 {
ans +=1;
flip +=1;
}
}
ans
}
}