Skip to content

Commit 401ff71

Browse files
committed
add solution of problem 319: bulb switcher
1 parent fcfb6d9 commit 401ff71

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

BulbSwitcher319/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
There are *n* bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the *i*th round, you toggle every *i* bulb. For the *n*th round, you only toggle the last bulb. Find how many bulbs are on after *n* rounds.
2+
3+
Example:
4+
```
5+
Given n = 3.
6+
7+
At first, the three bulbs are [off, off, off].
8+
After first round, the three bulbs are [on, on, on].
9+
After second round, the three bulbs are [on, off, on].
10+
After third round, the three bulbs are [on, off, off].
11+
12+
So you should return 1, because there is only one bulb is on.
13+
```

BulbSwitcher319/Solution.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Solution {
2+
public int bulbSwitch(int n) {
3+
if (n < 0)
4+
return Integer.MIN_VALUE;
5+
6+
int cnt = 0;
7+
8+
int value = 1;
9+
while (value * value <= n) {
10+
cnt++;
11+
value++;
12+
}
13+
14+
return cnt;
15+
}
16+
}

0 commit comments

Comments
 (0)