Skip to content

Commit

Permalink
672 solved.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Dec 2, 2017
1 parent 9ef2fa1 commit 034510f
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 0672-Bulb-Switcher-II/cpp-0672/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.5)
project(cpp_0672)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main2.cpp)
add_executable(cpp_0672 ${SOURCE_FILES})
63 changes: 63 additions & 0 deletions 0672-Bulb-Switcher-II/cpp-0672/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/// Source : https://leetcode.com/problems/bulb-switcher-ii/description/
/// Author : liuyubobobo
/// Time : 2017-12-02

#include <iostream>
#include <unordered_set>

using namespace std;

/// We can see the first six bulb status can decide all the sequence
/// So we can try all 2^4 operations and see how many status are there
/// for the first six bulb
///
/// Time Complexity: O(2^4)
/// Space Complexity: O(1)
class Solution {
public:
int flipLights(int n, int m) {

unordered_set<int> states;
n = min(n, 6);
for(int i = 0; i < 16 ; i++){
int state = 0b111111;

int onebit = onenum(i, 4);
if(onebit > m || m % 2 != onebit % 2)
continue;

if(i&1)
state ^= 0b111111;
if(i&2)
state ^= 0b101010;
if(i&4)
state ^= 0b010101;
if(i&8)
state ^= 0b001001;

states.insert(state&((1<<n)-1));
}

return states.size();
}

private:
int onenum(int x, int bitnum){
int res = 0;
for(int i = 0 ; i < bitnum ; i ++)
if(x&(1<<i))
res ++;
return res;
}
};

int main() {

cout << Solution().flipLights(1, 1) << endl;
cout << Solution().flipLights(2, 1) << endl;
cout << Solution().flipLights(3, 1) << endl;
cout << Solution().flipLights(3, 3) << endl;
cout << Solution().flipLights(3, 2) << endl;

return 0;
}
49 changes: 49 additions & 0 deletions 0672-Bulb-Switcher-II/cpp-0672/main2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// Source : https://leetcode.com/problems/bulb-switcher-ii/description/
/// Author : liuyubobobo
/// Time : 2017-12-02

#include <iostream>
#include <unordered_set>

using namespace std;

/// For further analysis, we can see the first three lights uniquely determine the rest of the sequence
/// Assume the four operation is a, b, c, d, then:
/// - Light 1 = 1 + a + c + d
/// - Light 2 = 1 + a + b
/// - Light 3 = 1 + a + c
/// - Light 4 = 1 + a + b + d
/// - Light 5 = 1 + a + c
/// - Light 6 = 1 + a + b
///
/// so that (module 2)
/// - Light 4 = (Light 1) + (Light 2) + (Light 3)
/// - Light 5 = Light 3
/// - Light 6 = Light 2
///
/// so there's just 8 cases, we can enumerate all these 8 cases and get the result.
///
/// Time Complexity: O(1)
/// Space Complexity: O(1)
class Solution {
public:
int flipLights(int n, int m) {

n = min(n, 3);
if (m == 0) return 1;
if (m == 1) return n == 1 ? 2 : n == 2 ? 3 : 4;
if (m == 2) return n == 1 ? 2 : n == 2 ? 4 : 7;
return n == 1 ? 2 : n == 2 ? 4 : 8;
}
};

int main() {

cout << Solution().flipLights(1, 1) << endl;
cout << Solution().flipLights(2, 1) << endl;
cout << Solution().flipLights(3, 1) << endl;
cout << Solution().flipLights(3, 3) << endl;
cout << Solution().flipLights(3, 2) << endl;

return 0;
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ email: [[email protected]](mailto:[email protected])
| | | | | | |
| 648 | [Replace Words](https://leetcode.com/problems/replace-words/description/) | [] | [C++](0648-Replace-Words/cpp-0648/) | | |
| | | | | | |
| 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/) | | |
| 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/) | | |
| 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/) | | [C++](0674-Longest-Continuous-Increasing-Subsequence/cpp-0674/) | | |
| 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/) | | |
Expand Down

0 comments on commit 034510f

Please sign in to comment.