Skip to content

Commit b00dfb3

Browse files
authored
feat: add solutions to lc problems: No.0995~0999 (#2039)
* No.0995.Minimum Number of K Consecutive Bit Flips * No.0997.Find the Town Judge * No.0998.Maximum Binary Tree II * No.0999.Available Captures for Rook
1 parent bcff22f commit b00dfb3

File tree

14 files changed

+518
-5
lines changed

14 files changed

+518
-5
lines changed

solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README.md

+145-1
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,166 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:差分数组**
60+
61+
我们注意到,对于若干个连续的元素进行反转,其结果与对这些元素进行反转的次序无关。因此我们可以贪心地考虑每个位置需要进行反转的次数。
62+
63+
我们不妨从左到右处理数组。
64+
65+
假设当前我们需要处理位置 $i$,而位置 $i$ 左侧的元素已经被处理完毕,如果 $i$ 位置的元素为 $0$,那么我们必须进行反转操作,我们需要将 $[i,..i+k-1]$ 区间内的元素进行反转。这里我们用一个差分数组 $d$ 来维护每个位置的反转次数,那么判断当前位置 $i$ 是否需要反转,只需要看 $s = \sum_{j=0}^{i}d[j]$ 以及 $nums[i]$ 的奇偶性,如果 $s$ 与 $nums[i]$ 奇偶性相同,那么位置 $i$ 的元素仍然为 $0$,需要进行反转。此时我们判断一下 $i+k$ 是否超出了数组的长度,如果超出了数组的长度,那么就无法完成目标,返回 $-1$。否则我们令 $d[i]$ 增加 $1$,同时令 $d[i+k]$ 减少 $1$,然后将答案增加 $1$,并且 $s$ 增加 $1$。
66+
67+
这样当我们处理完数组中的所有元素时,返回答案即可。
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。这里 $n$ 是数组 $nums$ 的长度。
70+
5971
<!-- tabs:start -->
6072

6173
### **Python3**
6274

6375
<!-- 这里可写当前语言的特殊实现逻辑 -->
6476

6577
```python
66-
78+
class Solution:
79+
def minKBitFlips(self, nums: List[int], k: int) -> int:
80+
n = len(nums)
81+
d = [0] * (n + 1)
82+
ans = s = 0
83+
for i, x in enumerate(nums):
84+
s += d[i]
85+
if x % 2 == s % 2:
86+
if i + k > n:
87+
return -1
88+
d[i] += 1
89+
d[i + k] -= 1
90+
s += 1
91+
ans += 1
92+
return ans
6793
```
6894

6995
### **Java**
7096

7197
<!-- 这里可写当前语言的特殊实现逻辑 -->
7298

7399
```java
100+
class Solution {
101+
public int minKBitFlips(int[] nums, int k) {
102+
int n = nums.length;
103+
int[] d = new int[n + 1];
104+
int ans = 0, s = 0;
105+
for (int i = 0; i < n; ++i) {
106+
s += d[i];
107+
if (nums[i] % 2 == s % 2) {
108+
if (i + k > n) {
109+
return -1;
110+
}
111+
++d[i];
112+
--d[i + k];
113+
++s;
114+
++ans;
115+
}
116+
}
117+
return ans;
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
int minKBitFlips(vector<int>& nums, int k) {
128+
int n = nums.size();
129+
int d[n + 1];
130+
memset(d, 0, sizeof(d));
131+
int ans = 0, s = 0;
132+
for (int i = 0; i < n; ++i) {
133+
s += d[i];
134+
if (s % 2 == nums[i] % 2) {
135+
if (i + k > n) {
136+
return -1;
137+
}
138+
++d[i];
139+
--d[i + k];
140+
++s;
141+
++ans;
142+
}
143+
}
144+
return ans;
145+
}
146+
};
147+
```
148+
149+
### **Go**
150+
151+
```go
152+
func minKBitFlips(nums []int, k int) int {
153+
n := len(nums)
154+
d := make([]int, n+1)
155+
ans, s := 0, 0
156+
for i, x := range nums {
157+
s += d[i]
158+
if s%2 == x%2 {
159+
if i+k > n {
160+
return -1
161+
}
162+
d[i]++
163+
d[i+k]--
164+
s++
165+
ans++
166+
}
167+
}
168+
return ans
169+
}
170+
```
171+
172+
### **TypeScript**
173+
174+
```ts
175+
function minKBitFlips(nums: number[], k: number): number {
176+
const n = nums.length;
177+
const d: number[] = Array(n + 1).fill(0);
178+
let [ans, s] = [0, 0];
179+
for (let i = 0; i < n; ++i) {
180+
s += d[i];
181+
if (s % 2 === nums[i] % 2) {
182+
if (i + k > n) {
183+
return -1;
184+
}
185+
d[i]++;
186+
d[i + k]--;
187+
s++;
188+
ans++;
189+
}
190+
}
191+
return ans;
192+
}
193+
```
74194

195+
### **Rust**
196+
197+
```rust
198+
impl Solution {
199+
pub fn min_k_bit_flips(nums: Vec<i32>, k: i32) -> i32 {
200+
let n = nums.len();
201+
let mut d = vec![0; n + 1];
202+
let mut ans = 0;
203+
let mut s = 0;
204+
for i in 0..n {
205+
s += d[i];
206+
if nums[i] % 2 == s % 2 {
207+
if i + (k as usize) > n {
208+
return -1;
209+
}
210+
d[i] += 1;
211+
d[i + (k as usize)] -= 1;
212+
s += 1;
213+
ans += 1;
214+
}
215+
}
216+
ans
217+
}
218+
}
75219
```
76220

77221
### **...**

solution/0900-0999/0995.Minimum Number of K Consecutive Bit Flips/README_EN.md

+145-1
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,162 @@ Flip nums[5],nums[6],nums[7]: nums becomes [1,1,1,1,1,1,1,1]
5050

5151
## Solutions
5252

53+
**Solution 1: Difference Array**
54+
55+
We notice that the result of reversing several consecutive elements is independent of the order of the reversals. Therefore, we can greedily consider the number of reversals needed at each position.
56+
57+
We can process the array from left to right.
58+
59+
Suppose we need to process position $i$, and the elements to the left of position $i$ have been processed. If the element at position $i$ is $0$, then we must perform a reversal operation, we need to reverse the elements in the interval $[i,..i+k-1]$. Here we use a difference array $d$ to maintain the number of reversals at each position, then to determine whether the current position $i$ needs to be reversed, we only need to see $s = \sum_{j=0}^{i}d[j]$ and the parity of $nums[i]$. If $s$ and $nums[i]$ have the same parity, then the element at position $i$ is still $0$ and needs to be reversed. At this time, we check whether $i+k$ exceeds the length of the array. If it exceeds the length of the array, then the target cannot be achieved, return $-1$. Otherwise, we increase $d[i]$ by $1$, decrease $d[i+k]$ by $1$, increase the answer by $1$, and increase $s$ by $1$.
60+
61+
In this way, when we have processed all the elements in the array, we can return the answer.
62+
63+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
64+
5365
<!-- tabs:start -->
5466

5567
### **Python3**
5668

5769
```python
58-
70+
class Solution:
71+
def minKBitFlips(self, nums: List[int], k: int) -> int:
72+
n = len(nums)
73+
d = [0] * (n + 1)
74+
ans = s = 0
75+
for i, x in enumerate(nums):
76+
s += d[i]
77+
if x % 2 == s % 2:
78+
if i + k > n:
79+
return -1
80+
d[i] += 1
81+
d[i + k] -= 1
82+
s += 1
83+
ans += 1
84+
return ans
5985
```
6086

6187
### **Java**
6288

6389
```java
90+
class Solution {
91+
public int minKBitFlips(int[] nums, int k) {
92+
int n = nums.length;
93+
int[] d = new int[n + 1];
94+
int ans = 0, s = 0;
95+
for (int i = 0; i < n; ++i) {
96+
s += d[i];
97+
if (nums[i] % 2 == s % 2) {
98+
if (i + k > n) {
99+
return -1;
100+
}
101+
++d[i];
102+
--d[i + k];
103+
++s;
104+
++ans;
105+
}
106+
}
107+
return ans;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int minKBitFlips(vector<int>& nums, int k) {
118+
int n = nums.size();
119+
int d[n + 1];
120+
memset(d, 0, sizeof(d));
121+
int ans = 0, s = 0;
122+
for (int i = 0; i < n; ++i) {
123+
s += d[i];
124+
if (s % 2 == nums[i] % 2) {
125+
if (i + k > n) {
126+
return -1;
127+
}
128+
++d[i];
129+
--d[i + k];
130+
++s;
131+
++ans;
132+
}
133+
}
134+
return ans;
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
func minKBitFlips(nums []int, k int) int {
143+
n := len(nums)
144+
d := make([]int, n+1)
145+
ans, s := 0, 0
146+
for i, x := range nums {
147+
s += d[i]
148+
if s%2 == x%2 {
149+
if i+k > n {
150+
return -1
151+
}
152+
d[i]++
153+
d[i+k]--
154+
s++
155+
ans++
156+
}
157+
}
158+
return ans
159+
}
160+
```
161+
162+
### **TypeScript**
163+
164+
```ts
165+
function minKBitFlips(nums: number[], k: number): number {
166+
const n = nums.length;
167+
const d: number[] = Array(n + 1).fill(0);
168+
let [ans, s] = [0, 0];
169+
for (let i = 0; i < n; ++i) {
170+
s += d[i];
171+
if (s % 2 === nums[i] % 2) {
172+
if (i + k > n) {
173+
return -1;
174+
}
175+
d[i]++;
176+
d[i + k]--;
177+
s++;
178+
ans++;
179+
}
180+
}
181+
return ans;
182+
}
183+
```
64184

185+
### **Rust**
186+
187+
```rust
188+
impl Solution {
189+
pub fn min_k_bit_flips(nums: Vec<i32>, k: i32) -> i32 {
190+
let n = nums.len();
191+
let mut d = vec![0; n + 1];
192+
let mut ans = 0;
193+
let mut s = 0;
194+
for i in 0..n {
195+
s += d[i];
196+
if nums[i] % 2 == s % 2 {
197+
if i + (k as usize) > n {
198+
return -1;
199+
}
200+
d[i] += 1;
201+
d[i + (k as usize)] -= 1;
202+
s += 1;
203+
ans += 1;
204+
}
205+
}
206+
ans
207+
}
208+
}
65209
```
66210

67211
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public:
3+
int minKBitFlips(vector<int>& nums, int k) {
4+
int n = nums.size();
5+
int d[n + 1];
6+
memset(d, 0, sizeof(d));
7+
int ans = 0, s = 0;
8+
for (int i = 0; i < n; ++i) {
9+
s += d[i];
10+
if (s % 2 == nums[i] % 2) {
11+
if (i + k > n) {
12+
return -1;
13+
}
14+
++d[i];
15+
--d[i + k];
16+
++s;
17+
++ans;
18+
}
19+
}
20+
return ans;
21+
}
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func minKBitFlips(nums []int, k int) int {
2+
n := len(nums)
3+
d := make([]int, n+1)
4+
ans, s := 0, 0
5+
for i, x := range nums {
6+
s += d[i]
7+
if s%2 == x%2 {
8+
if i+k > n {
9+
return -1
10+
}
11+
d[i]++
12+
d[i+k]--
13+
s++
14+
ans++
15+
}
16+
}
17+
return ans
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int minKBitFlips(int[] nums, int k) {
3+
int n = nums.length;
4+
int[] d = new int[n + 1];
5+
int ans = 0, s = 0;
6+
for (int i = 0; i < n; ++i) {
7+
s += d[i];
8+
if (nums[i] % 2 == s % 2) {
9+
if (i + k > n) {
10+
return -1;
11+
}
12+
++d[i];
13+
--d[i + k];
14+
++s;
15+
++ans;
16+
}
17+
}
18+
return ans;
19+
}
20+
}

0 commit comments

Comments
 (0)