Skip to content

Commit 9ef2fa1

Browse files
committed
319 solved.
1 parent a472459 commit 9ef2fa1

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(cpp_0319)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main.cpp)
7+
add_executable(cpp_0319 ${SOURCE_FILES})
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// Source : https://leetcode.com/problems/bulb-switcher/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-12-01
4+
5+
#include <iostream>
6+
#include <cassert>
7+
8+
using namespace std;
9+
10+
/// For every number, see whether the divisor number is odd
11+
/// Time Complexity: O(n*sqrt(n))
12+
/// Space Complexity: O(1)
13+
///
14+
/// Time Limit Exceed!!!
15+
class Solution {
16+
17+
public:
18+
int bulbSwitch(int n) {
19+
20+
assert(n >= 0);
21+
22+
if(n <= 1)
23+
return n;
24+
25+
int count = 1;
26+
for(int i = 2 ; i <= n ; i ++)
27+
if(divisorNumber(i) % 2 == 1)
28+
count += 1;
29+
return count;
30+
}
31+
32+
private:
33+
int divisorNumber(int x){
34+
int ret = 0;
35+
for(int i = 1 ; i * i <= x ; i ++)
36+
if(x % i == 0){
37+
ret += 1;
38+
if(x / i != i)
39+
ret += 1;
40+
}
41+
return ret;
42+
}
43+
};
44+
45+
int main() {
46+
47+
cout << Solution().bulbSwitch(2) << endl;
48+
cout << Solution().bulbSwitch(3) << endl;
49+
50+
return 0;
51+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// Source : https://leetcode.com/problems/bulb-switcher/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-12-01
4+
5+
#include <iostream>
6+
#include <cmath>
7+
#include <cassert>
8+
9+
using namespace std;
10+
11+
/// Only square number have odd divisor number!
12+
/// Time Complexity: O(1)
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
16+
public:
17+
int bulbSwitch(int n) {
18+
assert(n >= 0);
19+
return (int)sqrt(n);
20+
}
21+
};
22+
23+
int main() {
24+
25+
cout << Solution().bulbSwitch(2) << endl;
26+
cout << Solution().bulbSwitch(3) << endl;
27+
28+
return 0;
29+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ email: [[email protected]](mailto:[email protected])
134134
| 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/description/) | | [C++](0308-Range-Sum-Query-2D-Mutable/cpp-0308/) | | |
135135
| 309 | [Best Time to Buy and Sell Stock with Cooldown](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/description/) | | [C++](0309-Best-Time-to-Buy-and-Sell-Stock-with-Cooldown/cpp-0309/) | | |
136136
| | | | | | |
137+
| 319 | [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/description/) | [] | [C++](0319-Bulb-Switcher/cpp-0319/) | | |
138+
| | | | | | |
137139
| 343 | [Integer Break](https://leetcode.com/problems/integer-break/description/) | [] | [C++](0343-Integer-Break/cpp-0343/) | [Java](0343-Integer-Break/java-0343/src/) | |
138140
| | | | | | |
139141
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/description/) | [] | [C++](0347-Top-K-Frequent-Elements/cpp-0347/) | [Java](0347-Top-K-Frequent-Elements/java-0347/src/) | |

0 commit comments

Comments
 (0)