Skip to content

Commit 82a2749

Browse files
authored
feat: add solutions to lc problem: No.2939 (doocs#1986)
No.2939.Maximum Xor Product
1 parent bc650d2 commit 82a2749

File tree

7 files changed

+327
-6
lines changed

7 files changed

+327
-6
lines changed

solution/2900-2999/2939.Maximum Xor Product/README.md

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,146 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:贪心 + 位运算**
57+
58+
根据题目描述,我们可以给 $a$ 和 $b$ 在二进制下 $[0..n)$ 位上同时分配一个数字,最终使得 $a$ 和 $b$ 的乘积最大。
59+
60+
因此,我们首先提取 $a$ 和 $b$ 高于 $n$ 位的部分,分别记为 $ax$ 和 $bx$。
61+
62+
接下来,从大到小考虑 $[0..n)$ 位上的每一位,我们将 $a$ 和 $b$ 的当前位分别记为 $x$ 和 $y$。
63+
64+
如果 $x = y$,那么我们可以将 $ax$ 和 $bx$ 的当前位同时置为 $1$,因此,我们更新 $ax = ax \mid 1 << i$ 和 $bx = bx \mid 1 << i$。否则,如果 $ax \lt bx$,要使得最终的乘积最大,我们应该让 $ax$ 的当前位置为 $1$,否则我们可以将 $bx$ 的当前位置为 $1$。
65+
66+
最后,我们返回 $ax \times bx \bmod (10^9 + 7)$ 即为答案。
67+
68+
时间复杂度 $O(n)$,其中 $n$ 为题目给定的整数。空间复杂度 $O(1)$。
69+
5670
<!-- tabs:start -->
5771

5872
### **Python3**
5973

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

6276
```python
63-
77+
class Solution:
78+
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
79+
mod = 10**9 + 7
80+
ax, bx = (a >> n) << n, (b >> n) << n
81+
for i in range(n - 1, -1, -1):
82+
x = a >> i & 1
83+
y = b >> i & 1
84+
if x == y:
85+
ax |= 1 << i
86+
bx |= 1 << i
87+
elif ax > bx:
88+
bx |= 1 << i
89+
else:
90+
ax |= 1 << i
91+
return ax * bx % mod
6492
```
6593

6694
### **Java**
6795

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

7098
```java
71-
99+
class Solution {
100+
public int maximumXorProduct(long a, long b, int n) {
101+
final int mod = (int) 1e9 + 7;
102+
long ax = (a >> n) << n;
103+
long bx = (b >> n) << n;
104+
for (int i = n - 1; i >= 0; --i) {
105+
long x = a >> i & 1;
106+
long y = b >> i & 1;
107+
if (x == y) {
108+
ax |= 1L << i;
109+
bx |= 1L << i;
110+
} else if (ax < bx) {
111+
ax |= 1L << i;
112+
} else {
113+
bx |= 1L << i;
114+
}
115+
}
116+
ax %= mod;
117+
bx %= mod;
118+
return (int) (ax * bx % mod);
119+
}
120+
}
72121
```
73122

74123
### **C++**
75124

76125
```cpp
77-
126+
class Solution {
127+
public:
128+
int maximumXorProduct(long long a, long long b, int n) {
129+
const int mod = 1e9 + 7;
130+
long long ax = (a >> n) << n, bx = (b >> n) << n;
131+
for (int i = n - 1; ~i; --i) {
132+
int x = a >> i & 1, y = b >> i & 1;
133+
if (x == y) {
134+
ax |= 1LL << i;
135+
bx |= 1LL << i;
136+
} else if (ax < bx) {
137+
ax |= 1LL << i;
138+
} else {
139+
bx |= 1LL << i;
140+
}
141+
}
142+
ax %= mod;
143+
bx %= mod;
144+
return ax * bx % mod;
145+
}
146+
};
78147
```
79148
80149
### **Go**
81150
82151
```go
152+
func maximumXorProduct(a int64, b int64, n int) int {
153+
const mod int64 = 1e9 + 7
154+
ax := (a >> n) << n
155+
bx := (b >> n) << n
156+
for i := n - 1; i >= 0; i-- {
157+
x, y := (a>>i)&1, (b>>i)&1
158+
if x == y {
159+
ax |= 1 << i
160+
bx |= 1 << i
161+
} else if ax < bx {
162+
ax |= 1 << i
163+
} else {
164+
bx |= 1 << i
165+
}
166+
}
167+
ax %= mod
168+
bx %= mod
169+
return int(ax * bx % mod)
170+
}
171+
```
83172

173+
### **TypeScript**
174+
175+
```ts
176+
function maximumXorProduct(a: number, b: number, n: number): number {
177+
const mod = BigInt(1e9 + 7);
178+
let ax = (BigInt(a) >> BigInt(n)) << BigInt(n);
179+
let bx = (BigInt(b) >> BigInt(n)) << BigInt(n);
180+
for (let i = BigInt(n - 1); ~i; --i) {
181+
const x = (BigInt(a) >> i) & 1n;
182+
const y = (BigInt(b) >> i) & 1n;
183+
if (x === y) {
184+
ax |= 1n << i;
185+
bx |= 1n << i;
186+
} else if (ax < bx) {
187+
ax |= 1n << i;
188+
} else {
189+
bx |= 1n << i;
190+
}
191+
}
192+
ax %= mod;
193+
bx %= mod;
194+
return Number((ax * bx) % mod);
195+
}
84196
```
85197

86198
### **...**

solution/2900-2999/2939.Maximum Xor Product/README_EN.md

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,142 @@ It can be shown that 12 is the maximum value of (a XOR x) * (b XOR x) for all 0
4747

4848
## Solutions
4949

50+
**Solution 1: Greedy + Bitwise Operation**
51+
52+
According to the problem description, we can assign a number to the $[0..n)$ bits of $a$ and $b$ in binary at the same time, so that the product of $a$ and $b$ is maximized.
53+
54+
Therefore, we first extract the parts of $a$ and $b$ that are higher than the $n$ bits, denoted as $ax$ and $bx$.
55+
56+
Next, we consider each bit in $[0..n)$ from high to low. We denote the current bits of $a$ and $b$ as $x$ and $y$.
57+
58+
If $x = y$, then we can set the current bit of $ax$ and $bx$ to $1$ at the same time. Therefore, we update $ax = ax \mid 1 << i$ and $bx = bx \mid 1 << i$. Otherwise, if $ax < bx$, to maximize the final product, we should set the current bit of $ax$ to $1$. Otherwise, we can set the current bit of $bx$ to $1$.
59+
60+
Finally, we return $ax \times bx \bmod (10^9 + 7)$ as the answer.
61+
62+
The time complexity is $O(n)$, where $n$ is the integer given in the problem. The space complexity is $O(1)$.
63+
5064
<!-- tabs:start -->
5165

5266
### **Python3**
5367

5468
```python
55-
69+
class Solution:
70+
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
71+
mod = 10**9 + 7
72+
ax, bx = (a >> n) << n, (b >> n) << n
73+
for i in range(n - 1, -1, -1):
74+
x = a >> i & 1
75+
y = b >> i & 1
76+
if x == y:
77+
ax |= 1 << i
78+
bx |= 1 << i
79+
elif ax > bx:
80+
bx |= 1 << i
81+
else:
82+
ax |= 1 << i
83+
return ax * bx % mod
5684
```
5785

5886
### **Java**
5987

6088
```java
61-
89+
class Solution {
90+
public int maximumXorProduct(long a, long b, int n) {
91+
final int mod = (int) 1e9 + 7;
92+
long ax = (a >> n) << n;
93+
long bx = (b >> n) << n;
94+
for (int i = n - 1; i >= 0; --i) {
95+
long x = a >> i & 1;
96+
long y = b >> i & 1;
97+
if (x == y) {
98+
ax |= 1L << i;
99+
bx |= 1L << i;
100+
} else if (ax < bx) {
101+
ax |= 1L << i;
102+
} else {
103+
bx |= 1L << i;
104+
}
105+
}
106+
ax %= mod;
107+
bx %= mod;
108+
return (int) (ax * bx % mod);
109+
}
110+
}
62111
```
63112

64113
### **C++**
65114

66115
```cpp
67-
116+
class Solution {
117+
public:
118+
int maximumXorProduct(long long a, long long b, int n) {
119+
const int mod = 1e9 + 7;
120+
long long ax = (a >> n) << n, bx = (b >> n) << n;
121+
for (int i = n - 1; ~i; --i) {
122+
int x = a >> i & 1, y = b >> i & 1;
123+
if (x == y) {
124+
ax |= 1LL << i;
125+
bx |= 1LL << i;
126+
} else if (ax < bx) {
127+
ax |= 1LL << i;
128+
} else {
129+
bx |= 1LL << i;
130+
}
131+
}
132+
ax %= mod;
133+
bx %= mod;
134+
return ax * bx % mod;
135+
}
136+
};
68137
```
69138
70139
### **Go**
71140
72141
```go
142+
func maximumXorProduct(a int64, b int64, n int) int {
143+
const mod int64 = 1e9 + 7
144+
ax := (a >> n) << n
145+
bx := (b >> n) << n
146+
for i := n - 1; i >= 0; i-- {
147+
x, y := (a>>i)&1, (b>>i)&1
148+
if x == y {
149+
ax |= 1 << i
150+
bx |= 1 << i
151+
} else if ax < bx {
152+
ax |= 1 << i
153+
} else {
154+
bx |= 1 << i
155+
}
156+
}
157+
ax %= mod
158+
bx %= mod
159+
return int(ax * bx % mod)
160+
}
161+
```
73162

163+
### **TypeScript**
164+
165+
```ts
166+
function maximumXorProduct(a: number, b: number, n: number): number {
167+
const mod = BigInt(1e9 + 7);
168+
let ax = (BigInt(a) >> BigInt(n)) << BigInt(n);
169+
let bx = (BigInt(b) >> BigInt(n)) << BigInt(n);
170+
for (let i = BigInt(n - 1); ~i; --i) {
171+
const x = (BigInt(a) >> i) & 1n;
172+
const y = (BigInt(b) >> i) & 1n;
173+
if (x === y) {
174+
ax |= 1n << i;
175+
bx |= 1n << i;
176+
} else if (ax < bx) {
177+
ax |= 1n << i;
178+
} else {
179+
bx |= 1n << i;
180+
}
181+
}
182+
ax %= mod;
183+
bx %= mod;
184+
return Number((ax * bx) % mod);
185+
}
74186
```
75187

76188
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maximumXorProduct(long long a, long long b, int n) {
4+
const int mod = 1e9 + 7;
5+
long long ax = (a >> n) << n, bx = (b >> n) << n;
6+
for (int i = n - 1; ~i; --i) {
7+
int x = a >> i & 1, y = b >> i & 1;
8+
if (x == y) {
9+
ax |= 1LL << i;
10+
bx |= 1LL << i;
11+
} else if (ax < bx) {
12+
ax |= 1LL << i;
13+
} else {
14+
bx |= 1LL << i;
15+
}
16+
}
17+
ax %= mod;
18+
bx %= mod;
19+
return ax * bx % mod;
20+
}
21+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
func maximumXorProduct(a int64, b int64, n int) int {
2+
const mod int64 = 1e9 + 7
3+
ax := (a >> n) << n
4+
bx := (b >> n) << n
5+
for i := n - 1; i >= 0; i-- {
6+
x, y := (a>>i)&1, (b>>i)&1
7+
if x == y {
8+
ax |= 1 << i
9+
bx |= 1 << i
10+
} else if ax < bx {
11+
ax |= 1 << i
12+
} else {
13+
bx |= 1 << i
14+
}
15+
}
16+
ax %= mod
17+
bx %= mod
18+
return int(ax * bx % mod)
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int maximumXorProduct(long a, long b, int n) {
3+
final int mod = (int) 1e9 + 7;
4+
long ax = (a >> n) << n;
5+
long bx = (b >> n) << n;
6+
for (int i = n - 1; i >= 0; --i) {
7+
long x = a >> i & 1;
8+
long y = b >> i & 1;
9+
if (x == y) {
10+
ax |= 1L << i;
11+
bx |= 1L << i;
12+
} else if (ax < bx) {
13+
ax |= 1L << i;
14+
} else {
15+
bx |= 1L << i;
16+
}
17+
}
18+
ax %= mod;
19+
bx %= mod;
20+
return (int) (ax * bx % mod);
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
3+
mod = 10**9 + 7
4+
ax, bx = (a >> n) << n, (b >> n) << n
5+
for i in range(n - 1, -1, -1):
6+
x = a >> i & 1
7+
y = b >> i & 1
8+
if x == y:
9+
ax |= 1 << i
10+
bx |= 1 << i
11+
elif ax > bx:
12+
bx |= 1 << i
13+
else:
14+
ax |= 1 << i
15+
return ax * bx % mod

0 commit comments

Comments
 (0)