Skip to content

Commit 78ce43c

Browse files
committed
0995 solved.
1 parent add5514 commit 78ce43c

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(C)
3+
4+
set(CMAKE_CXX_STANDARD 11)
5+
6+
add_executable(C main2.cpp)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/// Source : https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-16
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Greedy + Simulation
12+
/// Time Complexity: O((N - K) * K)
13+
/// Space Complexity: O(1)
14+
class Solution {
15+
public:
16+
int minKBitFlips(vector<int>& A, int K) {
17+
18+
int res = 0;
19+
for(int i = 0; i < A.size(); i ++)
20+
if(A[i] == 0){
21+
if(i + K > A.size()) return -1;
22+
23+
res ++;
24+
for(int j = 0; j < K; j ++)
25+
A[i + j] = 1 - A[i + j];
26+
}
27+
return res;
28+
}
29+
};
30+
31+
32+
int main() {
33+
34+
vector<int> A1 = {0,1,0};
35+
cout << Solution().minKBitFlips(A1, 1) << endl;
36+
// 2
37+
38+
vector<int> A2 = {1,1,0};
39+
cout << Solution().minKBitFlips(A2, 2) << endl;
40+
// -1
41+
42+
vector<int> A3 = {0,0,0,1,0,1,1,0};
43+
cout << Solution().minKBitFlips(A3, 3) << endl;
44+
// 3
45+
46+
return 0;
47+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// Source : https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-18
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Greedy + Events
12+
/// Record every closed event
13+
/// Time Complexity: O(N)
14+
/// Space Complexity: O(N)
15+
class Solution {
16+
public:
17+
int minKBitFlips(vector<int>& A, int K) {
18+
19+
int res = 0;
20+
vector<bool> event(A.size(), false);
21+
int flip = 0;
22+
23+
for(int i = 0; i < A.size(); i ++){
24+
if((A[i] && flip % 2) || (!A[i] && flip % 2 == 0)){
25+
26+
if(i + K - 1 >= A.size()) return -1;
27+
28+
res ++;
29+
30+
flip ++;
31+
event[i + K - 1] = true;
32+
}
33+
34+
if(event[i]) flip --;
35+
}
36+
return res;
37+
}
38+
};
39+
40+
41+
int main() {
42+
43+
vector<int> A1 = {0,1,0};
44+
cout << Solution().minKBitFlips(A1, 1) << endl;
45+
// 2
46+
47+
vector<int> A2 = {1,1,0};
48+
cout << Solution().minKBitFlips(A2, 2) << endl;
49+
// -1
50+
51+
vector<int> A3 = {0,0,0,1,0,1,1,0};
52+
cout << Solution().minKBitFlips(A3, 3) << endl;
53+
// 3
54+
55+
return 0;
56+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,4 +659,5 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
659659
| 992 | [Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/) | [solution](https://leetcode.com/problems/subarrays-with-k-different-integers/solution/) | [C++](0992-Subarrays-with-K-Different-Integers/cpp-0992/) | | |
660660
| 993 | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [solution](https://leetcode.com/problems/cousins-in-binary-tree/solution/) | [C++](0993-Cousins-in-Binary-Tree/cpp-0993/) | | |
661661
| 994 | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [solution](https://leetcode.com/problems/rotting-oranges/solution/) | [C++](0994-Rotting-Oranges/cpp-0994/) | | |
662+
| 995 | [Minimum Number of K Consecutive Bit Flips](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/) | [solution](https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/solution/) | [C++](0995-Minimum-Number-of-K-Consecutive-Bit-Flips/cpp-0995/) | | |
662663
| | | | | | |

0 commit comments

Comments
 (0)