Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.2939 (#1986)
Browse files Browse the repository at this point in the history
No.2939.Maximum Xor Product
  • Loading branch information
yanglbme authored Nov 19, 2023
1 parent bc650d2 commit 82a2749
Show file tree
Hide file tree
Showing 7 changed files with 327 additions and 6 deletions.
118 changes: 115 additions & 3 deletions solution/2900-2999/2939.Maximum Xor Product/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,146 @@

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

**方法一:贪心 + 位运算**

根据题目描述,我们可以给 $a$ 和 $b$ 在二进制下 $[0..n)$ 位上同时分配一个数字,最终使得 $a$ 和 $b$ 的乘积最大。

因此,我们首先提取 $a$ 和 $b$ 高于 $n$ 位的部分,分别记为 $ax$ 和 $bx$。

接下来,从大到小考虑 $[0..n)$ 位上的每一位,我们将 $a$ 和 $b$ 的当前位分别记为 $x$ 和 $y$。

如果 $x = y$,那么我们可以将 $ax$ 和 $bx$ 的当前位同时置为 $1$,因此,我们更新 $ax = ax \mid 1 << i$ 和 $bx = bx \mid 1 << i$。否则,如果 $ax \lt bx$,要使得最终的乘积最大,我们应该让 $ax$ 的当前位置为 $1$,否则我们可以将 $bx$ 的当前位置为 $1$。

最后,我们返回 $ax \times bx \bmod (10^9 + 7)$ 即为答案。

时间复杂度 $O(n)$,其中 $n$ 为题目给定的整数。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

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

```python

class Solution:
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
mod = 10**9 + 7
ax, bx = (a >> n) << n, (b >> n) << n
for i in range(n - 1, -1, -1):
x = a >> i & 1
y = b >> i & 1
if x == y:
ax |= 1 << i
bx |= 1 << i
elif ax > bx:
bx |= 1 << i
else:
ax |= 1 << i
return ax * bx % mod
```

### **Java**

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

```java

class Solution {
public int maximumXorProduct(long a, long b, int n) {
final int mod = (int) 1e9 + 7;
long ax = (a >> n) << n;
long bx = (b >> n) << n;
for (int i = n - 1; i >= 0; --i) {
long x = a >> i & 1;
long y = b >> i & 1;
if (x == y) {
ax |= 1L << i;
bx |= 1L << i;
} else if (ax < bx) {
ax |= 1L << i;
} else {
bx |= 1L << i;
}
}
ax %= mod;
bx %= mod;
return (int) (ax * bx % mod);
}
}
```

### **C++**

```cpp

class Solution {
public:
int maximumXorProduct(long long a, long long b, int n) {
const int mod = 1e9 + 7;
long long ax = (a >> n) << n, bx = (b >> n) << n;
for (int i = n - 1; ~i; --i) {
int x = a >> i & 1, y = b >> i & 1;
if (x == y) {
ax |= 1LL << i;
bx |= 1LL << i;
} else if (ax < bx) {
ax |= 1LL << i;
} else {
bx |= 1LL << i;
}
}
ax %= mod;
bx %= mod;
return ax * bx % mod;
}
};
```
### **Go**
```go
func maximumXorProduct(a int64, b int64, n int) int {
const mod int64 = 1e9 + 7
ax := (a >> n) << n
bx := (b >> n) << n
for i := n - 1; i >= 0; i-- {
x, y := (a>>i)&1, (b>>i)&1
if x == y {
ax |= 1 << i
bx |= 1 << i
} else if ax < bx {
ax |= 1 << i
} else {
bx |= 1 << i
}
}
ax %= mod
bx %= mod
return int(ax * bx % mod)
}
```

### **TypeScript**

```ts
function maximumXorProduct(a: number, b: number, n: number): number {
const mod = BigInt(1e9 + 7);
let ax = (BigInt(a) >> BigInt(n)) << BigInt(n);
let bx = (BigInt(b) >> BigInt(n)) << BigInt(n);
for (let i = BigInt(n - 1); ~i; --i) {
const x = (BigInt(a) >> i) & 1n;
const y = (BigInt(b) >> i) & 1n;
if (x === y) {
ax |= 1n << i;
bx |= 1n << i;
} else if (ax < bx) {
ax |= 1n << i;
} else {
bx |= 1n << i;
}
}
ax %= mod;
bx %= mod;
return Number((ax * bx) % mod);
}
```

### **...**
Expand Down
118 changes: 115 additions & 3 deletions solution/2900-2999/2939.Maximum Xor Product/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,142 @@ It can be shown that 12 is the maximum value of (a XOR x) * (b XOR x) for all 0

## Solutions

**Solution 1: Greedy + Bitwise Operation**

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.

Therefore, we first extract the parts of $a$ and $b$ that are higher than the $n$ bits, denoted as $ax$ and $bx$.

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$.

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$.

Finally, we return $ax \times bx \bmod (10^9 + 7)$ as the answer.

The time complexity is $O(n)$, where $n$ is the integer given in the problem. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**

```python

class Solution:
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
mod = 10**9 + 7
ax, bx = (a >> n) << n, (b >> n) << n
for i in range(n - 1, -1, -1):
x = a >> i & 1
y = b >> i & 1
if x == y:
ax |= 1 << i
bx |= 1 << i
elif ax > bx:
bx |= 1 << i
else:
ax |= 1 << i
return ax * bx % mod
```

### **Java**

```java

class Solution {
public int maximumXorProduct(long a, long b, int n) {
final int mod = (int) 1e9 + 7;
long ax = (a >> n) << n;
long bx = (b >> n) << n;
for (int i = n - 1; i >= 0; --i) {
long x = a >> i & 1;
long y = b >> i & 1;
if (x == y) {
ax |= 1L << i;
bx |= 1L << i;
} else if (ax < bx) {
ax |= 1L << i;
} else {
bx |= 1L << i;
}
}
ax %= mod;
bx %= mod;
return (int) (ax * bx % mod);
}
}
```

### **C++**

```cpp

class Solution {
public:
int maximumXorProduct(long long a, long long b, int n) {
const int mod = 1e9 + 7;
long long ax = (a >> n) << n, bx = (b >> n) << n;
for (int i = n - 1; ~i; --i) {
int x = a >> i & 1, y = b >> i & 1;
if (x == y) {
ax |= 1LL << i;
bx |= 1LL << i;
} else if (ax < bx) {
ax |= 1LL << i;
} else {
bx |= 1LL << i;
}
}
ax %= mod;
bx %= mod;
return ax * bx % mod;
}
};
```
### **Go**
```go
func maximumXorProduct(a int64, b int64, n int) int {
const mod int64 = 1e9 + 7
ax := (a >> n) << n
bx := (b >> n) << n
for i := n - 1; i >= 0; i-- {
x, y := (a>>i)&1, (b>>i)&1
if x == y {
ax |= 1 << i
bx |= 1 << i
} else if ax < bx {
ax |= 1 << i
} else {
bx |= 1 << i
}
}
ax %= mod
bx %= mod
return int(ax * bx % mod)
}
```

### **TypeScript**

```ts
function maximumXorProduct(a: number, b: number, n: number): number {
const mod = BigInt(1e9 + 7);
let ax = (BigInt(a) >> BigInt(n)) << BigInt(n);
let bx = (BigInt(b) >> BigInt(n)) << BigInt(n);
for (let i = BigInt(n - 1); ~i; --i) {
const x = (BigInt(a) >> i) & 1n;
const y = (BigInt(b) >> i) & 1n;
if (x === y) {
ax |= 1n << i;
bx |= 1n << i;
} else if (ax < bx) {
ax |= 1n << i;
} else {
bx |= 1n << i;
}
}
ax %= mod;
bx %= mod;
return Number((ax * bx) % mod);
}
```

### **...**
Expand Down
21 changes: 21 additions & 0 deletions solution/2900-2999/2939.Maximum Xor Product/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
int maximumXorProduct(long long a, long long b, int n) {
const int mod = 1e9 + 7;
long long ax = (a >> n) << n, bx = (b >> n) << n;
for (int i = n - 1; ~i; --i) {
int x = a >> i & 1, y = b >> i & 1;
if (x == y) {
ax |= 1LL << i;
bx |= 1LL << i;
} else if (ax < bx) {
ax |= 1LL << i;
} else {
bx |= 1LL << i;
}
}
ax %= mod;
bx %= mod;
return ax * bx % mod;
}
};
19 changes: 19 additions & 0 deletions solution/2900-2999/2939.Maximum Xor Product/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
func maximumXorProduct(a int64, b int64, n int) int {
const mod int64 = 1e9 + 7
ax := (a >> n) << n
bx := (b >> n) << n
for i := n - 1; i >= 0; i-- {
x, y := (a>>i)&1, (b>>i)&1
if x == y {
ax |= 1 << i
bx |= 1 << i
} else if ax < bx {
ax |= 1 << i
} else {
bx |= 1 << i
}
}
ax %= mod
bx %= mod
return int(ax * bx % mod)
}
22 changes: 22 additions & 0 deletions solution/2900-2999/2939.Maximum Xor Product/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public int maximumXorProduct(long a, long b, int n) {
final int mod = (int) 1e9 + 7;
long ax = (a >> n) << n;
long bx = (b >> n) << n;
for (int i = n - 1; i >= 0; --i) {
long x = a >> i & 1;
long y = b >> i & 1;
if (x == y) {
ax |= 1L << i;
bx |= 1L << i;
} else if (ax < bx) {
ax |= 1L << i;
} else {
bx |= 1L << i;
}
}
ax %= mod;
bx %= mod;
return (int) (ax * bx % mod);
}
}
15 changes: 15 additions & 0 deletions solution/2900-2999/2939.Maximum Xor Product/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def maximumXorProduct(self, a: int, b: int, n: int) -> int:
mod = 10**9 + 7
ax, bx = (a >> n) << n, (b >> n) << n
for i in range(n - 1, -1, -1):
x = a >> i & 1
y = b >> i & 1
if x == y:
ax |= 1 << i
bx |= 1 << i
elif ax > bx:
bx |= 1 << i
else:
ax |= 1 << i
return ax * bx % mod
Loading

0 comments on commit 82a2749

Please sign in to comment.