Skip to content

Commit 4656458

Browse files
add 319
1 parent 9333700 commit 4656458

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ Your ideas/fixes/algorithms are more than welcome!
217217
|322|[Coin Change](https://leetcode.com/problems/coin-change/)|[Solution](../master/src/main/java/com/fishercoder/solutions/CoinChange.java)| O(?)|O(?) | Medium|
218218
|321|[Create Maximum Number](https://leetcode.com/problems/create-maximum-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/CreateMaximumNumber.java)| O(?)|O(?) | Hard
219219
|320|[Generalized Abbreviation](https://leetcode.com/problems/generalized-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/GeneralizedAbbreviation.java)| O(n*2^n)|O(n) | Medium| Backtracking, Bit Manipulation
220+
|319|[Bulb Switcher](https://leetcode.com/problems/bulb-switcher/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_319.java)| O(1)|O(1) | Medium| Brainteaser
220221
|317|[Shortest Distance from All Buildings](https://leetcode.com/problems/shortest-distance-from-all-buildings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/ShortestDistanceFromAllBuildings.java)| O(?)|O(?) | Hard|
221222
|316|[Remove Duplicate Letters](https://leetcode.com/problems/remove-duplicate-letters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_316.java)| O(n)|O(1)| Hard| Stack, Recursion, Greedy
222223
|315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)|[Solution](../master/src/main/java/com/fishercoder/solutions/CountOfSmallerNumbersAfterSelf.java)| O(?)|O(?)| Hard| Tree
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 319. Bulb Switcher
5+
*
6+
* There are n bulbs that are initially off. You first turn on all the bulbs.
7+
* Then, you turn off every second bulb. On the third round,
8+
* you toggle every third bulb (turning on if it's off or turning off if it's on).
9+
* For the ith round, you toggle every i bulb. For the nth round, you only
10+
* toggle the last bulb. Find how many bulbs are on after n rounds.
11+
12+
Example:
13+
14+
Given n = 3.
15+
16+
At first, the three bulbs are [off, off, off].
17+
After first round, the three bulbs are [on, on, on].
18+
After second round, the three bulbs are [on, off, on].
19+
After third round, the three bulbs are [on, off, off].
20+
21+
So you should return 1, because there is only one bulb is on.
22+
*/
23+
public class _319 {
24+
25+
public int bulbSwitch(int n) {
26+
if (n < 2) return n;
27+
return (int) Math.sqrt(n);
28+
}
29+
30+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._319;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
/**
10+
* Created by stevesun on 6/6/17.
11+
*/
12+
public class _319Test {
13+
private static _319 test;
14+
15+
@BeforeClass
16+
public static void setup(){
17+
test = new _319();
18+
}
19+
20+
@Test
21+
public void test1(){
22+
assertEquals(1, test.bulbSwitch(2));
23+
}
24+
25+
@Test
26+
public void test2(){
27+
assertEquals(1, test.bulbSwitch(3));
28+
}
29+
30+
@Test
31+
public void test3(){
32+
assertEquals(2, test.bulbSwitch(4));
33+
}
34+
35+
@Test
36+
public void test4(){
37+
assertEquals(2, test.bulbSwitch(5));
38+
}
39+
40+
@Test
41+
public void test5(){
42+
assertEquals(2, test.bulbSwitch(6));
43+
}
44+
45+
@Test
46+
public void test6(){
47+
assertEquals(2, test.bulbSwitch(7));
48+
}
49+
50+
@Test
51+
public void test7(){
52+
assertEquals(2, test.bulbSwitch(8));
53+
}
54+
55+
@Test
56+
public void test8(){
57+
assertEquals(3, test.bulbSwitch(9));
58+
}
59+
60+
@Test
61+
public void test11(){
62+
assertEquals(3, test.bulbSwitch(15));
63+
}
64+
65+
@Test
66+
public void test9(){
67+
assertEquals(4, test.bulbSwitch(17));
68+
}
69+
70+
@Test
71+
public void test10(){
72+
assertEquals(4, test.bulbSwitch(16));
73+
}
74+
75+
76+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy