Skip to content

Commit c5c070d

Browse files
authored
feat: add solutions to lc problem: No.3022 (doocs#2282)
No.3022. Minimize OR of Remaining Elements Using Operations
1 parent 4a331cb commit c5c070d

File tree

6 files changed

+294
-8
lines changed

6 files changed

+294
-8
lines changed

solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README.md

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,113 @@
6565
<!-- tabs:start -->
6666

6767
```python
68-
68+
class Solution:
69+
def minOrAfterOperations(self, nums: List[int], k: int) -> int:
70+
ans = 0
71+
rans = 0
72+
for i in range(29, -1, -1):
73+
test = ans + (1 << i)
74+
cnt = 0
75+
val = 0
76+
for num in nums:
77+
if val == 0:
78+
val = test & num
79+
else:
80+
val &= test & num
81+
if val:
82+
cnt += 1
83+
if cnt > k:
84+
rans += 1 << i
85+
else:
86+
ans += 1 << i
87+
return rans
6988
```
7089

7190
```java
72-
91+
class Solution {
92+
public int minOrAfterOperations(int[] nums, int k) {
93+
int ans = 0, rans = 0;
94+
for (int i = 29; i >= 0; i--) {
95+
int test = ans + (1 << i);
96+
int cnt = 0;
97+
int val = 0;
98+
for (int num : nums) {
99+
if (val == 0) {
100+
val = test & num;
101+
} else {
102+
val &= test & num;
103+
}
104+
if (val != 0) {
105+
cnt++;
106+
}
107+
}
108+
if (cnt > k) {
109+
rans += (1 << i);
110+
} else {
111+
ans += (1 << i);
112+
}
113+
}
114+
return rans;
115+
}
116+
}
73117
```
74118

75119
```cpp
76-
120+
class Solution {
121+
public:
122+
int minOrAfterOperations(vector<int>& nums, int k) {
123+
int ans = 0, rans = 0;
124+
for (int i = 29; i >= 0; i--) {
125+
int test = ans + (1 << i);
126+
int cnt = 0;
127+
int val = 0;
128+
for (auto it : nums) {
129+
if (val == 0) {
130+
val = test & it;
131+
} else {
132+
val &= test & it;
133+
}
134+
if (val) {
135+
cnt++;
136+
}
137+
}
138+
if (cnt > k) {
139+
rans += (1 << i);
140+
} else {
141+
ans += (1 << i);
142+
}
143+
}
144+
return rans;
145+
}
146+
};
77147
```
78148
79149
```go
80-
150+
func minOrAfterOperations(nums []int, k int) int {
151+
ans := 0
152+
rans := 0
153+
for i := 29; i >= 0; i-- {
154+
test := ans + (1 << i)
155+
cnt := 0
156+
val := 0
157+
for _, num := range nums {
158+
if val == 0 {
159+
val = test & num
160+
} else {
161+
val &= test & num
162+
}
163+
if val != 0 {
164+
cnt++
165+
}
166+
}
167+
if cnt > k {
168+
rans += (1 << i)
169+
} else {
170+
ans += (1 << i)
171+
}
172+
}
173+
return rans
174+
}
81175
```
82176

83177
<!-- tabs:end -->

solution/3000-3099/3022.Minimize OR of Remaining Elements Using Operations/README_EN.md

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,113 @@ It can be shown that 15 is the minimum possible value of the bitwise OR of the r
6161
<!-- tabs:start -->
6262

6363
```python
64-
64+
class Solution:
65+
def minOrAfterOperations(self, nums: List[int], k: int) -> int:
66+
ans = 0
67+
rans = 0
68+
for i in range(29, -1, -1):
69+
test = ans + (1 << i)
70+
cnt = 0
71+
val = 0
72+
for num in nums:
73+
if val == 0:
74+
val = test & num
75+
else:
76+
val &= test & num
77+
if val:
78+
cnt += 1
79+
if cnt > k:
80+
rans += 1 << i
81+
else:
82+
ans += 1 << i
83+
return rans
6584
```
6685

6786
```java
68-
87+
class Solution {
88+
public int minOrAfterOperations(int[] nums, int k) {
89+
int ans = 0, rans = 0;
90+
for (int i = 29; i >= 0; i--) {
91+
int test = ans + (1 << i);
92+
int cnt = 0;
93+
int val = 0;
94+
for (int num : nums) {
95+
if (val == 0) {
96+
val = test & num;
97+
} else {
98+
val &= test & num;
99+
}
100+
if (val != 0) {
101+
cnt++;
102+
}
103+
}
104+
if (cnt > k) {
105+
rans += (1 << i);
106+
} else {
107+
ans += (1 << i);
108+
}
109+
}
110+
return rans;
111+
}
112+
}
69113
```
70114

71115
```cpp
72-
116+
class Solution {
117+
public:
118+
int minOrAfterOperations(vector<int>& nums, int k) {
119+
int ans = 0, rans = 0;
120+
for (int i = 29; i >= 0; i--) {
121+
int test = ans + (1 << i);
122+
int cnt = 0;
123+
int val = 0;
124+
for (auto it : nums) {
125+
if (val == 0) {
126+
val = test & it;
127+
} else {
128+
val &= test & it;
129+
}
130+
if (val) {
131+
cnt++;
132+
}
133+
}
134+
if (cnt > k) {
135+
rans += (1 << i);
136+
} else {
137+
ans += (1 << i);
138+
}
139+
}
140+
return rans;
141+
}
142+
};
73143
```
74144
75145
```go
76-
146+
func minOrAfterOperations(nums []int, k int) int {
147+
ans := 0
148+
rans := 0
149+
for i := 29; i >= 0; i-- {
150+
test := ans + (1 << i)
151+
cnt := 0
152+
val := 0
153+
for _, num := range nums {
154+
if val == 0 {
155+
val = test & num
156+
} else {
157+
val &= test & num
158+
}
159+
if val != 0 {
160+
cnt++
161+
}
162+
}
163+
if cnt > k {
164+
rans += (1 << i)
165+
} else {
166+
ans += (1 << i)
167+
}
168+
}
169+
return rans
170+
}
77171
```
78172

79173
<!-- tabs:end -->
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int minOrAfterOperations(vector<int>& nums, int k) {
4+
int ans = 0, rans = 0;
5+
for (int i = 29; i >= 0; i--) {
6+
int test = ans + (1 << i);
7+
int cnt = 0;
8+
int val = 0;
9+
for (auto it : nums) {
10+
if (val == 0) {
11+
val = test & it;
12+
} else {
13+
val &= test & it;
14+
}
15+
if (val) {
16+
cnt++;
17+
}
18+
}
19+
if (cnt > k) {
20+
rans += (1 << i);
21+
} else {
22+
ans += (1 << i);
23+
}
24+
}
25+
return rans;
26+
}
27+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func minOrAfterOperations(nums []int, k int) int {
2+
ans := 0
3+
rans := 0
4+
for i := 29; i >= 0; i-- {
5+
test := ans + (1 << i)
6+
cnt := 0
7+
val := 0
8+
for _, num := range nums {
9+
if val == 0 {
10+
val = test & num
11+
} else {
12+
val &= test & num
13+
}
14+
if val != 0 {
15+
cnt++
16+
}
17+
}
18+
if cnt > k {
19+
rans += (1 << i)
20+
} else {
21+
ans += (1 << i)
22+
}
23+
}
24+
return rans
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int minOrAfterOperations(int[] nums, int k) {
3+
int ans = 0, rans = 0;
4+
for (int i = 29; i >= 0; i--) {
5+
int test = ans + (1 << i);
6+
int cnt = 0;
7+
int val = 0;
8+
for (int num : nums) {
9+
if (val == 0) {
10+
val = test & num;
11+
} else {
12+
val &= test & num;
13+
}
14+
if (val != 0) {
15+
cnt++;
16+
}
17+
}
18+
if (cnt > k) {
19+
rans += (1 << i);
20+
} else {
21+
ans += (1 << i);
22+
}
23+
}
24+
return rans;
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def minOrAfterOperations(self, nums: List[int], k: int) -> int:
3+
ans = 0
4+
rans = 0
5+
for i in range(29, -1, -1):
6+
test = ans + (1 << i)
7+
cnt = 0
8+
val = 0
9+
for num in nums:
10+
if val == 0:
11+
val = test & num
12+
else:
13+
val &= test & num
14+
if val:
15+
cnt += 1
16+
if cnt > k:
17+
rans += 1 << i
18+
else:
19+
ans += 1 << i
20+
return rans

0 commit comments

Comments
 (0)