Skip to content

Commit 034510f

Browse files
committed
672 solved.
1 parent 9ef2fa1 commit 034510f

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-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_0672)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main2.cpp)
7+
add_executable(cpp_0672 ${SOURCE_FILES})
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// Source : https://leetcode.com/problems/bulb-switcher-ii/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-12-02
4+
5+
#include <iostream>
6+
#include <unordered_set>
7+
8+
using namespace std;
9+
10+
/// We can see the first six bulb status can decide all the sequence
11+
/// So we can try all 2^4 operations and see how many status are there
12+
/// for the first six bulb
13+
///
14+
/// Time Complexity: O(2^4)
15+
/// Space Complexity: O(1)
16+
class Solution {
17+
public:
18+
int flipLights(int n, int m) {
19+
20+
unordered_set<int> states;
21+
n = min(n, 6);
22+
for(int i = 0; i < 16 ; i++){
23+
int state = 0b111111;
24+
25+
int onebit = onenum(i, 4);
26+
if(onebit > m || m % 2 != onebit % 2)
27+
continue;
28+
29+
if(i&1)
30+
state ^= 0b111111;
31+
if(i&2)
32+
state ^= 0b101010;
33+
if(i&4)
34+
state ^= 0b010101;
35+
if(i&8)
36+
state ^= 0b001001;
37+
38+
states.insert(state&((1<<n)-1));
39+
}
40+
41+
return states.size();
42+
}
43+
44+
private:
45+
int onenum(int x, int bitnum){
46+
int res = 0;
47+
for(int i = 0 ; i < bitnum ; i ++)
48+
if(x&(1<<i))
49+
res ++;
50+
return res;
51+
}
52+
};
53+
54+
int main() {
55+
56+
cout << Solution().flipLights(1, 1) << endl;
57+
cout << Solution().flipLights(2, 1) << endl;
58+
cout << Solution().flipLights(3, 1) << endl;
59+
cout << Solution().flipLights(3, 3) << endl;
60+
cout << Solution().flipLights(3, 2) << endl;
61+
62+
return 0;
63+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// Source : https://leetcode.com/problems/bulb-switcher-ii/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2017-12-02
4+
5+
#include <iostream>
6+
#include <unordered_set>
7+
8+
using namespace std;
9+
10+
/// For further analysis, we can see the first three lights uniquely determine the rest of the sequence
11+
/// Assume the four operation is a, b, c, d, then:
12+
/// - Light 1 = 1 + a + c + d
13+
/// - Light 2 = 1 + a + b
14+
/// - Light 3 = 1 + a + c
15+
/// - Light 4 = 1 + a + b + d
16+
/// - Light 5 = 1 + a + c
17+
/// - Light 6 = 1 + a + b
18+
///
19+
/// so that (module 2)
20+
/// - Light 4 = (Light 1) + (Light 2) + (Light 3)
21+
/// - Light 5 = Light 3
22+
/// - Light 6 = Light 2
23+
///
24+
/// so there's just 8 cases, we can enumerate all these 8 cases and get the result.
25+
///
26+
/// Time Complexity: O(1)
27+
/// Space Complexity: O(1)
28+
class Solution {
29+
public:
30+
int flipLights(int n, int m) {
31+
32+
n = min(n, 3);
33+
if (m == 0) return 1;
34+
if (m == 1) return n == 1 ? 2 : n == 2 ? 3 : 4;
35+
if (m == 2) return n == 1 ? 2 : n == 2 ? 4 : 7;
36+
return n == 1 ? 2 : n == 2 ? 4 : 8;
37+
}
38+
};
39+
40+
int main() {
41+
42+
cout << Solution().flipLights(1, 1) << endl;
43+
cout << Solution().flipLights(2, 1) << endl;
44+
cout << Solution().flipLights(3, 1) << endl;
45+
cout << Solution().flipLights(3, 3) << endl;
46+
cout << Solution().flipLights(3, 2) << endl;
47+
48+
return 0;
49+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ email: [[email protected]](mailto:[email protected])
175175
| | | | | | |
176176
| 648 | [Replace Words](https://leetcode.com/problems/replace-words/description/) | [] | [C++](0648-Replace-Words/cpp-0648/) | | |
177177
| | | | | | |
178+
| 672 | [Bulb Switcher II](https://leetcode.com/problems/bulb-switcher-ii/description/) | [solution](https://leetcode.com/problems/bulb-switcher-ii/solution/) | [C++](0672-Bulb-Switcher-II/cpp-0672/) | | |
178179
| 673 | [Number of Longest Increasing Subsequence](https://leetcode.com/problems/number-of-longest-increasing-subsequence/description/) | [缺:BIT;线段树] | [C++](0673-Number-of-Longest-Increasing-Subsequence/cpp-0673/) | | |
179180
| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/) | | [C++](0674-Longest-Continuous-Increasing-Subsequence/cpp-0674/) | | |
180181
| 675 | [Cut Off Trees for Golf Event](https://leetcode.com/problems/cut-off-trees-for-golf-event/description/) | [缺:A*;Hadlock's Algo] | [C++](0675-Cut-Off-Trees-for-Golf-Event/cpp-0675/) | | |

0 commit comments

Comments
 (0)