Skip to content

Commit 932ae80

Browse files
authored
feat: add solutions to lc problems: No.2917,2918 (doocs#1903)
* No.2917.Find the K-or of an Array * No.2918.Minimum Equal Sum of Two Arrays After Replacing Zeros
1 parent 5d75407 commit 932ae80

File tree

14 files changed

+542
-12
lines changed

14 files changed

+542
-12
lines changed

Diff for: solution/2900-2999/2917.Find the K-or of an Array/README.md

+76-3
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,107 @@ nums[1]、nums[2]、nums[3]、nums[4] 和 nums[5] 的第 3 位的值为 1 。
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
**方法一:枚举**
65+
66+
我们可以在 $[0, 32)$ 范围内枚举每一位 $i$,统计数组 $nums$ 有多少个数的第 $i$ 位为 $1$,记为 $cnt$。如果 $cnt \ge k$,那么我们就将 $2^i$ 次方加到答案中。
67+
68+
枚举结束后,返回答案即可。
69+
70+
时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组 $nums$ 的长度以及 $nums$ 的最大值。空间复杂度 $O(1)$。
71+
6472
<!-- tabs:start -->
6573

6674
### **Python3**
6775

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

7078
```python
71-
79+
class Solution:
80+
def findKOr(self, nums: List[int], k: int) -> int:
81+
ans = 0
82+
for i in range(32):
83+
cnt = sum(x >> i & 1 for x in nums)
84+
if cnt >= k:
85+
ans |= 1 << i
86+
return ans
7287
```
7388

7489
### **Java**
7590

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

7893
```java
79-
94+
class Solution {
95+
public int findKOr(int[] nums, int k) {
96+
int ans = 0;
97+
for (int i = 0; i < 32; ++i) {
98+
int cnt = 0;
99+
for (int x : nums) {
100+
cnt += (x >> i & 1);
101+
}
102+
if (cnt >= k) {
103+
ans |= 1 << i;
104+
}
105+
}
106+
return ans;
107+
}
108+
}
80109
```
81110

82111
### **C++**
83112

84113
```cpp
85-
114+
class Solution {
115+
public:
116+
int findKOr(vector<int>& nums, int k) {
117+
int ans = 0;
118+
for (int i = 0; i < 32; ++i) {
119+
int cnt = 0;
120+
for (int x : nums) {
121+
cnt += (x >> i & 1);
122+
}
123+
if (cnt >= k) {
124+
ans |= 1 << i;
125+
}
126+
}
127+
return ans;
128+
}
129+
};
86130
```
87131
88132
### **Go**
89133
90134
```go
135+
func findKOr(nums []int, k int) (ans int) {
136+
for i := 0; i < 32; i++ {
137+
cnt := 0
138+
for _, x := range nums {
139+
cnt += (x >> i & 1)
140+
}
141+
if cnt >= k {
142+
ans |= 1 << i
143+
}
144+
}
145+
return
146+
}
147+
```
91148

149+
### **TypeScript**
150+
151+
```ts
152+
function findKOr(nums: number[], k: number): number {
153+
let ans = 0;
154+
for (let i = 0; i < 32; ++i) {
155+
let cnt = 0;
156+
for (const x of nums) {
157+
cnt += (x >> i) & 1;
158+
}
159+
if (cnt >= k) {
160+
ans |= 1 << i;
161+
}
162+
}
163+
return ans;
164+
}
92165
```
93166

94167
### **...**

Diff for: solution/2900-2999/2917.Find the K-or of an Array/README_EN.md

+76-3
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,103 @@ Only bits 0 and 3 are set in at least k elements of the array, and bits i &gt;=
5656

5757
## Solutions
5858

59+
**Solution 1: Enumeration**
60+
61+
We can enumerate each bit $i$ in the range $[0, 32)$, and count the number of numbers in the array $nums$ whose $i$-th bit is $1$, denoted as $cnt$. If $cnt \ge k$, we add $2^i$ to the answer.
62+
63+
After the enumeration, we return the answer.
64+
65+
The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the length of the array $nums$ and the maximum value in $nums$, respectively. The space complexity is $O(1)$.
66+
5967
<!-- tabs:start -->
6068

6169
### **Python3**
6270

6371
```python
64-
72+
class Solution:
73+
def findKOr(self, nums: List[int], k: int) -> int:
74+
ans = 0
75+
for i in range(32):
76+
cnt = sum(x >> i & 1 for x in nums)
77+
if cnt >= k:
78+
ans |= 1 << i
79+
return ans
6580
```
6681

6782
### **Java**
6883

6984
```java
70-
85+
class Solution {
86+
public int findKOr(int[] nums, int k) {
87+
int ans = 0;
88+
for (int i = 0; i < 32; ++i) {
89+
int cnt = 0;
90+
for (int x : nums) {
91+
cnt += (x >> i & 1);
92+
}
93+
if (cnt >= k) {
94+
ans |= 1 << i;
95+
}
96+
}
97+
return ans;
98+
}
99+
}
71100
```
72101

73102
### **C++**
74103

75104
```cpp
76-
105+
class Solution {
106+
public:
107+
int findKOr(vector<int>& nums, int k) {
108+
int ans = 0;
109+
for (int i = 0; i < 32; ++i) {
110+
int cnt = 0;
111+
for (int x : nums) {
112+
cnt += (x >> i & 1);
113+
}
114+
if (cnt >= k) {
115+
ans |= 1 << i;
116+
}
117+
}
118+
return ans;
119+
}
120+
};
77121
```
78122
79123
### **Go**
80124
81125
```go
126+
func findKOr(nums []int, k int) (ans int) {
127+
for i := 0; i < 32; i++ {
128+
cnt := 0
129+
for _, x := range nums {
130+
cnt += (x >> i & 1)
131+
}
132+
if cnt >= k {
133+
ans |= 1 << i
134+
}
135+
}
136+
return
137+
}
138+
```
82139

140+
### **TypeScript**
141+
142+
```ts
143+
function findKOr(nums: number[], k: number): number {
144+
let ans = 0;
145+
for (let i = 0; i < 32; ++i) {
146+
let cnt = 0;
147+
for (const x of nums) {
148+
cnt += (x >> i) & 1;
149+
}
150+
if (cnt >= k) {
151+
ans |= 1 << i;
152+
}
153+
}
154+
return ans;
155+
}
83156
```
84157

85158
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int findKOr(vector<int>& nums, int k) {
4+
int ans = 0;
5+
for (int i = 0; i < 32; ++i) {
6+
int cnt = 0;
7+
for (int x : nums) {
8+
cnt += (x >> i & 1);
9+
}
10+
if (cnt >= k) {
11+
ans |= 1 << i;
12+
}
13+
}
14+
return ans;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func findKOr(nums []int, k int) (ans int) {
2+
for i := 0; i < 32; i++ {
3+
cnt := 0
4+
for _, x := range nums {
5+
cnt += (x >> i & 1)
6+
}
7+
if cnt >= k {
8+
ans |= 1 << i
9+
}
10+
}
11+
return
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int findKOr(int[] nums, int k) {
3+
int ans = 0;
4+
for (int i = 0; i < 32; ++i) {
5+
int cnt = 0;
6+
for (int x : nums) {
7+
cnt += (x >> i & 1);
8+
}
9+
if (cnt >= k) {
10+
ans |= 1 << i;
11+
}
12+
}
13+
return ans;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findKOr(self, nums: List[int], k: int) -> int:
3+
ans = 0
4+
for i in range(32):
5+
cnt = sum(x >> i & 1 for x in nums)
6+
if cnt >= k:
7+
ans |= 1 << i
8+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function findKOr(nums: number[], k: number): number {
2+
let ans = 0;
3+
for (let i = 0; i < 32; ++i) {
4+
let cnt = 0;
5+
for (const x of nums) {
6+
cnt += (x >> i) & 1;
7+
}
8+
if (cnt >= k) {
9+
ans |= 1 << i;
10+
}
11+
}
12+
return ans;
13+
}

0 commit comments

Comments
 (0)