Skip to content

Commit a1fec59

Browse files
authored
feat: add solutions to lc problem: No.2595 (doocs#2044)
No.2595.Number of Even and Odd Bits
1 parent d582d74 commit a1fec59

File tree

10 files changed

+184
-55
lines changed

10 files changed

+184
-55
lines changed

solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README_EN.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@
5454

5555
**Solution 1: Prefix XOR + Hash Table**
5656

57-
We observe that a subarray can become an array of all $0$ s if and only if the number of $1$s in each bit of all the elements in the subarray is even.
57+
We observe that a subarray can become an array of all $0$s if and only if the number of $1$s on each binary bit of all elements in the subarray is even.
5858

59-
If there are indices $i$ and $j$ such that $i \lt j$ and the number of $1$s in each bit of the subarray $nums[0,..,i]$ and $nums[0,..,j]$ is the same, then we can make the subarray $nums[i + 1,..,j]$ an array of all $0$ s.
59+
If there exist indices $i$ and $j$ such that $i \lt j$ and the subarrays $nums[0,..,i]$ and $nums[0,..,j]$ have the same parity of the number of $1$s on each binary bit, then we can turn the subarray $nums[i + 1,..,j]$ into an array of all $0$s.
6060

61-
Therefore, we can use the prefix XOR method and use the hash table $cnt$ to count the number of occurrences of each prefix XOR value. Traverse the array, for each element $x$, calculate the prefix XOR value $mask$, then add the number of occurrences of $mask$ to the answer. Then, add $1$ to the number of occurrences of $mask$.
61+
Therefore, we can use the prefix XOR method and a hash table $cnt$ to count the occurrences of each prefix XOR value. We traverse the array, for each element $x$, we calculate its prefix XOR value $mask$, then add the number of occurrences of $mask$ to the answer. Then, we increase the number of occurrences of $mask$ by $1$.
6262

63-
Finally, return the answer.
63+
Finally, we return the answer.
6464

65-
Time complexity $O(n)$, space complexity $O(n)$, where $n$ is the length of the array $nums$.
65+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
6666

6767
<!-- tabs:start -->
6868

solution/2500-2599/2589.Minimum Time to Complete All Tasks/README_EN.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ The computer will be on for a total of 4 seconds.
4747

4848
## Solutions
4949

50-
**Solution 1: Greedy + Sort**
50+
**Solution 1: Greedy + Sorting**
5151

52-
We find that the problem is equivalent to choosing $duration$ integer time points in each interval $[start,..,end]$ so that the total number of integer time points selected is the smallest.
52+
We observe that the problem is equivalent to selecting $duration$ integer time points in each interval $[start,..,end]$, so that the total number of selected integer time points is minimized.
5353

54-
Therefore, we can sort the $tasks$ by the end time $end$ from small to large. Then greedily select. For each task, we start from the end time $end$ and choose as many points as possible from back to front, so that these points are more likely to be reused by later tasks.
54+
Therefore, we can first sort $tasks$ in ascending order of end time $end$. Then we greedily make selections. For each task, we start from the end time $end$ and choose the points as late as possible from back to front. This way, these points are more likely to be reused by later tasks.
5555

56-
In implementation, we can use a length of $2010$ array $vis$ to record whether each time point has been selected. Then for each task, we first count the number of points that have been selected in the $[start,..,end]$ interval $cnt$, then choose $duration - cnt$ points from back to front, and record the number of points selected $ans$ and update the $vis$ array.
56+
In our implementation, we can use an array $vis$ of length $2010$ to record whether each time point has been selected. Then for each task, we first count the number of points $cnt$ that have been selected in the interval $[start,..,end]$, and then select $duration - cnt$ points from back to front, while recording the number of selected points $ans$ and updating the $vis$ array.
5757

5858
Finally, we return $ans$.
5959

60+
The time complexity is $O(n \times \log n + n \times m)$, and the space complexity is $O(m)$. Here, $n$ and $m$ are the lengths of $tasks$ and $vis$ array, respectively. In this problem, $m = 2010$.
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**

solution/2500-2599/2594.Minimum Time to Repair Cars/README_EN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ It can be proved that the cars cannot be repaired in less than 16 minutes.​​
5151

5252
**Solution 1: Binary Search**
5353

54-
We notice that the longer the repair time, the more repaired cars. Therefore, we can use binary search to find the minimum repair time.
54+
We notice that the longer the repair time, the more cars are repaired. Therefore, we can use the repair time as the target of binary search, and binary search for the minimum repair time.
5555

56-
We define the left and right boundaries of binary search as $left=0$, $right=ranks[0] \times cars \times cars$. Next, we enumerate the repair time $mid$ in binary search. The number of cars that each mechanic can repair is $\lfloor \sqrt{\frac{mid}{r}} \rfloor$, where $\lfloor x \rfloor$ represents the floor function. If the number of cars repaired is greater than or equal to $cars$, then the repair time $mid$ is feasible, and we shrink the right boundary to $mid$, otherwise we increase the left boundary to $mid+1$.
56+
We define the left and right boundaries of the binary search as $left=0$, $right=ranks[0] \times cars \times cars$. Next, we binary search for the repair time $mid$, and the number of cars each mechanic can repair is $\lfloor \sqrt{\frac{mid}{r}} \rfloor$, where $\lfloor x \rfloor$ represents rounding down. If the number of cars repaired is greater than or equal to $cars$, it means that the repair time $mid$ is feasible, we reduce the right boundary to $mid$, otherwise we increase the left boundary to $mid+1$.
5757

5858
Finally, we return the left boundary.
5959

60-
Time complexity $(n \times \log n)$, space complexity $O(1)$. Where $n$ is the number of mechanics.
60+
The time complexity is $O(n \times \log M)$, and the space complexity is $O(1)$. Here, $n$ is the number of mechanics, and $M$ is the upper bound of the binary search.
6161

6262
<!-- tabs:start -->
6363

solution/2500-2599/2595.Number of Even and Odd Bits/README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ class Solution:
7070
return ans
7171
```
7272

73+
```python
74+
class Solution:
75+
def evenOddBit(self, n: int) -> List[int]:
76+
mask = 0x5555
77+
even = (n & mask).bit_count()
78+
odd = (n & ~mask).bit_count()
79+
return [even, odd]
80+
```
81+
7382
### **Java**
7483

7584
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -86,6 +95,17 @@ class Solution {
8695
}
8796
```
8897

98+
```java
99+
class Solution {
100+
public int[] evenOddBit(int n) {
101+
int mask = 0x5555;
102+
int even = Integer.bitCount(n & mask);
103+
int odd = Integer.bitCount(n & ~mask);
104+
return new int[] {even, odd};
105+
}
106+
}
107+
```
108+
89109
### **C++**
90110

91111
```cpp
@@ -101,6 +121,18 @@ public:
101121
};
102122
```
103123
124+
```cpp
125+
class Solution {
126+
public:
127+
vector<int> evenOddBit(int n) {
128+
int mask = 0x5555;
129+
int even = __builtin_popcount(n & mask);
130+
int odd = __builtin_popcount(n & ~mask);
131+
return {even, odd};
132+
}
133+
};
134+
```
135+
104136
### **Go**
105137

106138
```go
@@ -113,6 +145,15 @@ func evenOddBit(n int) []int {
113145
}
114146
```
115147

148+
```go
149+
func evenOddBit(n int) []int {
150+
mask := 0x5555
151+
even := bits.OnesCount32(uint32(n & mask))
152+
odd := bits.OnesCount32(uint32(n & ^mask))
153+
return []int{even, odd}
154+
}
155+
```
156+
116157
### **TypeScript**
117158

118159
```ts
@@ -125,6 +166,24 @@ function evenOddBit(n: number): number[] {
125166
}
126167
```
127168

169+
```ts
170+
function evenOddBit(n: number): number[] {
171+
const mask = 0x5555;
172+
const even = bitCount(n & mask);
173+
const odd = bitCount(n & ~mask);
174+
return [even, odd];
175+
}
176+
177+
function bitCount(i: number): number {
178+
i = i - ((i >>> 1) & 0x55555555);
179+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
180+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
181+
i = i + (i >>> 8);
182+
i = i + (i >>> 16);
183+
return i & 0x3f;
184+
}
185+
```
186+
128187
### **Rust**
129188

130189
```rust
@@ -145,6 +204,17 @@ impl Solution {
145204
}
146205
```
147206

207+
```rust
208+
impl Solution {
209+
pub fn even_odd_bit(n: i32) -> Vec<i32> {
210+
let mask: i32 = 0x5555;
211+
let even = (n & mask).count_ones() as i32;
212+
let odd = (n & !mask).count_ones() as i32;
213+
vec![even, odd]
214+
}
215+
}
216+
```
217+
148218
### **...**
149219

150220
```

solution/2500-2599/2595.Number of Even and Odd Bits/README_EN.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ class Solution:
6464
return ans
6565
```
6666

67+
```python
68+
class Solution:
69+
def evenOddBit(self, n: int) -> List[int]:
70+
mask = 0x5555
71+
even = (n & mask).bit_count()
72+
odd = (n & ~mask).bit_count()
73+
return [even, odd]
74+
```
75+
6776
### **Java**
6877

6978
```java
@@ -78,6 +87,17 @@ class Solution {
7887
}
7988
```
8089

90+
```java
91+
class Solution {
92+
public int[] evenOddBit(int n) {
93+
int mask = 0x5555;
94+
int even = Integer.bitCount(n & mask);
95+
int odd = Integer.bitCount(n & ~mask);
96+
return new int[] {even, odd};
97+
}
98+
}
99+
```
100+
81101
### **C++**
82102

83103
```cpp
@@ -93,6 +113,18 @@ public:
93113
};
94114
```
95115
116+
```cpp
117+
class Solution {
118+
public:
119+
vector<int> evenOddBit(int n) {
120+
int mask = 0x5555;
121+
int even = __builtin_popcount(n & mask);
122+
int odd = __builtin_popcount(n & ~mask);
123+
return {even, odd};
124+
}
125+
};
126+
```
127+
96128
### **Go**
97129

98130
```go
@@ -105,6 +137,15 @@ func evenOddBit(n int) []int {
105137
}
106138
```
107139

140+
```go
141+
func evenOddBit(n int) []int {
142+
mask := 0x5555
143+
even := bits.OnesCount32(uint32(n & mask))
144+
odd := bits.OnesCount32(uint32(n & ^mask))
145+
return []int{even, odd}
146+
}
147+
```
148+
108149
### **TypeScript**
109150

110151
```ts
@@ -117,6 +158,24 @@ function evenOddBit(n: number): number[] {
117158
}
118159
```
119160

161+
```ts
162+
function evenOddBit(n: number): number[] {
163+
const mask = 0x5555;
164+
const even = bitCount(n & mask);
165+
const odd = bitCount(n & ~mask);
166+
return [even, odd];
167+
}
168+
169+
function bitCount(i: number): number {
170+
i = i - ((i >>> 1) & 0x55555555);
171+
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
172+
i = (i + (i >>> 4)) & 0x0f0f0f0f;
173+
i = i + (i >>> 8);
174+
i = i + (i >>> 16);
175+
return i & 0x3f;
176+
}
177+
```
178+
120179
### **Rust**
121180

122181
```rust
@@ -137,6 +196,17 @@ impl Solution {
137196
}
138197
```
139198

199+
```rust
200+
impl Solution {
201+
pub fn even_odd_bit(n: i32) -> Vec<i32> {
202+
let mask: i32 = 0x5555;
203+
let even = (n & mask).count_ones() as i32;
204+
let odd = (n & !mask).count_ones() as i32;
205+
vec![even, odd]
206+
}
207+
}
208+
```
209+
140210
### **...**
141211

142212
```
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
class Solution {
2-
public:
3-
vector<int> evenOddBit(int n) {
4-
vector<int> ans(2);
5-
for (int i = 0; n > 0; n >>= 1, i ^= 1) {
6-
ans[i] += n & 1;
7-
}
8-
return ans;
9-
}
1+
class Solution {
2+
public:
3+
vector<int> evenOddBit(int n) {
4+
int mask = 0x5555;
5+
int even = __builtin_popcount(n & mask);
6+
int odd = __builtin_popcount(n & ~mask);
7+
return {even, odd};
8+
}
109
};
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
func evenOddBit(n int) []int {
2-
ans := make([]int, 2)
3-
for i := 0; n != 0; n, i = n>>1, i^1 {
4-
ans[i] += n & 1
5-
}
6-
return ans
2+
mask := 0x5555
3+
even := bits.OnesCount32(uint32(n & mask))
4+
odd := bits.OnesCount32(uint32(n & ^mask))
5+
return []int{even, odd}
76
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
class Solution {
2-
public int[] evenOddBit(int n) {
3-
int[] ans = new int[2];
4-
for (int i = 0; n > 0; n >>= 1, i ^= 1) {
5-
ans[i] += n & 1;
6-
}
7-
return ans;
8-
}
1+
class Solution {
2+
public int[] evenOddBit(int n) {
3+
int mask = 0x5555;
4+
int even = Integer.bitCount(n & mask);
5+
int odd = Integer.bitCount(n & ~mask);
6+
return new int[] {even, odd};
7+
}
98
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
class Solution:
2-
def evenOddBit(self, n: int) -> List[int]:
3-
ans = [0, 0]
4-
i = 0
5-
while n:
6-
ans[i] += n & 1
7-
i ^= 1
8-
n >>= 1
9-
return ans
1+
class Solution:
2+
def evenOddBit(self, n: int) -> List[int]:
3+
mask = 0x5555
4+
even = (n & mask).bit_count()
5+
odd = (n & ~mask).bit_count()
6+
return [even, odd]
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
impl Solution {
2-
pub fn even_odd_bit(mut n: i32) -> Vec<i32> {
3-
let mut ans = vec![0; 2];
4-
5-
let mut i = 0;
6-
while n != 0 {
7-
ans[i] += n & 1;
8-
9-
n >>= 1;
10-
i ^= 1;
11-
}
12-
13-
ans
2+
pub fn even_odd_bit(n: i32) -> Vec<i32> {
3+
let mask: i32 = 0x5555;
4+
let even = (n & mask).count_ones() as i32;
5+
let odd = (n & !mask).count_ones() as i32;
6+
vec![even, odd]
147
}
158
}

0 commit comments

Comments
 (0)