Skip to content

Commit 15e49ce

Browse files
committed
feat: add solutions to lc problem: No.1829
No.1829.Maximum XOR for Each Query
1 parent 2e0c75c commit 15e49ce

File tree

7 files changed

+217
-121
lines changed

7 files changed

+217
-121
lines changed

solution/1800-1899/1829.Maximum XOR for Each Query/README.md

+80-41
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,15 @@
6464

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

67-
前缀异或。
67+
**方法一:位运算 + 枚举**
68+
69+
我们先预处理出数组 `nums` 的异或和 $xs$,即 $xs=nums[0] \oplus nums[1] \oplus \cdots \oplus nums[n-1]$。
70+
71+
接下来,我们从后往前枚举数组 `nums` 中的每个元素 $x$,当前的异或和为 $xs$,我们需要找到一个数 $k$,使得 $xs \oplus k$ 的值尽可能大,并且 $k \lt 2^{maximumBit}$。
72+
73+
也即是说,我们从 $xs$ 的第 $maximumBit - 1$ 位开始,往低位枚举,如果 $xs$ 的某一位为 $0$,那么我们就将 $k$ 的对应位设置为 $1$,否则我们就将 $k$ 的对应位设置为 $0$。这样,最终得到的 $k$ 就是每一次查询的答案。然后,我们将 $xs$ 更新为 $xs \oplus x$,继续枚举下一个元素。
74+
75+
时间复杂度 $O(n \times m)$,其中 $n$ 和 $m$ 分别是数组 `nums``maximumBit` 的值。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
6876

6977
<!-- tabs:start -->
7078

@@ -75,17 +83,15 @@
7583
```python
7684
class Solution:
7785
def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
78-
n = len(nums)
79-
s = [0] * (n + 1)
80-
for i, x in enumerate(nums):
81-
s[i + 1] = s[i] ^ x
8286
ans = []
83-
for x in s[:0:-1]:
84-
t = 0
85-
for i in range(maximumBit):
86-
if ((x >> i) & 1) == 0:
87-
t |= 1 << i
88-
ans.append(t)
87+
xs = reduce(xor, nums)
88+
for x in nums[::-1]:
89+
k = 0
90+
for i in range(maximumBit - 1, -1, -1):
91+
if (xs >> i & 1) == 0:
92+
k |= 1 << i
93+
ans.append(k)
94+
xs ^= x
8995
return ans
9096
```
9197

@@ -97,19 +103,21 @@ class Solution:
97103
class Solution {
98104
public int[] getMaximumXor(int[] nums, int maximumBit) {
99105
int n = nums.length;
100-
int[] s = new int[n + 1];
101-
for (int i = 0; i < n; ++i) {
102-
s[i + 1] = s[i] ^ nums[i];
106+
int xs = 0;
107+
for (int x : nums) {
108+
xs ^= x;
103109
}
104110
int[] ans = new int[n];
105-
for (int i = n; i > 0; --i) {
106-
int t = 0, x = s[i];
107-
for (int j = 0; j < maximumBit; ++j) {
108-
if (((x >> j) & 1) == 0) {
109-
t |= (1 << j);
111+
for (int i = 0; i < n; ++i) {
112+
int x = nums[n - i - 1];
113+
int k = 0;
114+
for (int j = maximumBit - 1; j >= 0; --j) {
115+
if (((xs >> j) & 1) == 0) {
116+
k |= 1 << j;
110117
}
111118
}
112-
ans[n - i] = t;
119+
ans[i] = k;
120+
xs ^= x;
113121
}
114122
return ans;
115123
}
@@ -122,16 +130,22 @@ class Solution {
122130
class Solution {
123131
public:
124132
vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
133+
int xs = 0;
134+
for (int& x : nums) {
135+
xs ^= x;
136+
}
125137
int n = nums.size();
126-
vector<int> s(n + 1);
127-
for (int i = 0; i < n; ++i) s[i + 1] = s[i] ^ nums[i];
128-
vector<int> ans;
129-
for (int i = n; i > 0; --i) {
130-
int t = 0, x = s[i];
131-
for (int j = 0; j < maximumBit; ++j) {
132-
if (((x >> j) & 1) == 0) t |= (1 << j);
138+
vector<int> ans(n);
139+
for (int i = 0; i < n; ++i) {
140+
int x = nums[n - i - 1];
141+
int k = 0;
142+
for (int j = maximumBit - 1; ~j; --j) {
143+
if ((xs >> j & 1) == 0) {
144+
k |= 1 << j;
145+
}
133146
}
134-
ans.push_back(t);
147+
ans[i] = k;
148+
xs ^= x;
135149
}
136150
return ans;
137151
}
@@ -141,23 +155,48 @@ public:
141155
### **Go**
142156
143157
```go
144-
func getMaximumXor(nums []int, maximumBit int) []int {
145-
n := len(nums)
146-
s := make([]int, n+1)
147-
for i, v := range nums {
148-
s[i+1] = s[i] ^ v
158+
func getMaximumXor(nums []int, maximumBit int) (ans []int) {
159+
xs := 0
160+
for _, x := range nums {
161+
xs ^= x
149162
}
150-
var ans []int
151-
for i := n; i > 0; i-- {
152-
t, x := 0, s[i]
153-
for j := 0; j < maximumBit; j++ {
154-
if ((x >> j) & 1) == 0 {
155-
t |= (1 << j)
163+
for i := range nums {
164+
x := nums[len(nums)-i-1]
165+
k := 0
166+
for j := maximumBit - 1; j >= 0; j-- {
167+
if xs>>j&1 == 0 {
168+
k |= 1 << j
156169
}
157170
}
158-
ans = append(ans, t)
171+
ans = append(ans, k)
172+
xs ^= x
159173
}
160-
return ans
174+
return
175+
}
176+
```
177+
178+
### **TypeScript**
179+
180+
```ts
181+
function getMaximumXor(nums: number[], maximumBit: number): number[] {
182+
let xs = 0;
183+
for (const x of nums) {
184+
xs ^= x;
185+
}
186+
const n = nums.length;
187+
const ans = new Array(n);
188+
for (let i = 0; i < n; ++i) {
189+
const x = nums[n - i - 1];
190+
let k = 0;
191+
for (let j = maximumBit - 1; j >= 0; --j) {
192+
if (((xs >> j) & 1) == 0) {
193+
k |= 1 << j;
194+
}
195+
}
196+
ans[i] = k;
197+
xs ^= x;
198+
}
199+
return ans;
161200
}
162201
```
163202

solution/1800-1899/1829.Maximum XOR for Each Query/README_EN.md

+71-40
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,15 @@
6565
```python
6666
class Solution:
6767
def getMaximumXor(self, nums: List[int], maximumBit: int) -> List[int]:
68-
n = len(nums)
69-
s = [0] * (n + 1)
70-
for i, x in enumerate(nums):
71-
s[i + 1] = s[i] ^ x
7268
ans = []
73-
for x in s[:0:-1]:
74-
t = 0
75-
for i in range(maximumBit):
76-
if ((x >> i) & 1) == 0:
77-
t |= 1 << i
78-
ans.append(t)
69+
xs = reduce(xor, nums)
70+
for x in nums[::-1]:
71+
k = 0
72+
for i in range(maximumBit - 1, -1, -1):
73+
if (xs >> i & 1) == 0:
74+
k |= 1 << i
75+
ans.append(k)
76+
xs ^= x
7977
return ans
8078
```
8179

@@ -85,19 +83,21 @@ class Solution:
8583
class Solution {
8684
public int[] getMaximumXor(int[] nums, int maximumBit) {
8785
int n = nums.length;
88-
int[] s = new int[n + 1];
89-
for (int i = 0; i < n; ++i) {
90-
s[i + 1] = s[i] ^ nums[i];
86+
int xs = 0;
87+
for (int x : nums) {
88+
xs ^= x;
9189
}
9290
int[] ans = new int[n];
93-
for (int i = n; i > 0; --i) {
94-
int t = 0, x = s[i];
95-
for (int j = 0; j < maximumBit; ++j) {
96-
if (((x >> j) & 1) == 0) {
97-
t |= (1 << j);
91+
for (int i = 0; i < n; ++i) {
92+
int x = nums[n - i - 1];
93+
int k = 0;
94+
for (int j = maximumBit - 1; j >= 0; --j) {
95+
if (((xs >> j) & 1) == 0) {
96+
k |= 1 << j;
9897
}
9998
}
100-
ans[n - i] = t;
99+
ans[i] = k;
100+
xs ^= x;
101101
}
102102
return ans;
103103
}
@@ -110,16 +110,22 @@ class Solution {
110110
class Solution {
111111
public:
112112
vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
113+
int xs = 0;
114+
for (int& x : nums) {
115+
xs ^= x;
116+
}
113117
int n = nums.size();
114-
vector<int> s(n + 1);
115-
for (int i = 0; i < n; ++i) s[i + 1] = s[i] ^ nums[i];
116-
vector<int> ans;
117-
for (int i = n; i > 0; --i) {
118-
int t = 0, x = s[i];
119-
for (int j = 0; j < maximumBit; ++j) {
120-
if (((x >> j) & 1) == 0) t |= (1 << j);
118+
vector<int> ans(n);
119+
for (int i = 0; i < n; ++i) {
120+
int x = nums[n - i - 1];
121+
int k = 0;
122+
for (int j = maximumBit - 1; ~j; --j) {
123+
if ((xs >> j & 1) == 0) {
124+
k |= 1 << j;
125+
}
121126
}
122-
ans.push_back(t);
127+
ans[i] = k;
128+
xs ^= x;
123129
}
124130
return ans;
125131
}
@@ -129,23 +135,48 @@ public:
129135
### **Go**
130136
131137
```go
132-
func getMaximumXor(nums []int, maximumBit int) []int {
133-
n := len(nums)
134-
s := make([]int, n+1)
135-
for i, v := range nums {
136-
s[i+1] = s[i] ^ v
138+
func getMaximumXor(nums []int, maximumBit int) (ans []int) {
139+
xs := 0
140+
for _, x := range nums {
141+
xs ^= x
137142
}
138-
var ans []int
139-
for i := n; i > 0; i-- {
140-
t, x := 0, s[i]
141-
for j := 0; j < maximumBit; j++ {
142-
if ((x >> j) & 1) == 0 {
143-
t |= (1 << j)
143+
for i := range nums {
144+
x := nums[len(nums)-i-1]
145+
k := 0
146+
for j := maximumBit - 1; j >= 0; j-- {
147+
if xs>>j&1 == 0 {
148+
k |= 1 << j
144149
}
145150
}
146-
ans = append(ans, t)
151+
ans = append(ans, k)
152+
xs ^= x
147153
}
148-
return ans
154+
return
155+
}
156+
```
157+
158+
### **TypeScript**
159+
160+
```ts
161+
function getMaximumXor(nums: number[], maximumBit: number): number[] {
162+
let xs = 0;
163+
for (const x of nums) {
164+
xs ^= x;
165+
}
166+
const n = nums.length;
167+
const ans = new Array(n);
168+
for (let i = 0; i < n; ++i) {
169+
const x = nums[n - i - 1];
170+
let k = 0;
171+
for (let j = maximumBit - 1; j >= 0; --j) {
172+
if (((xs >> j) & 1) == 0) {
173+
k |= 1 << j;
174+
}
175+
}
176+
ans[i] = k;
177+
xs ^= x;
178+
}
179+
return ans;
149180
}
150181
```
151182

solution/1800-1899/1829.Maximum XOR for Each Query/Solution.cpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
class Solution {
22
public:
33
vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
4+
int xs = 0;
5+
for (int& x : nums) {
6+
xs ^= x;
7+
}
48
int n = nums.size();
5-
vector<int> s(n + 1);
6-
for (int i = 0; i < n; ++i) s[i + 1] = s[i] ^ nums[i];
7-
vector<int> ans;
8-
for (int i = n; i > 0; --i) {
9-
int t = 0, x = s[i];
10-
for (int j = 0; j < maximumBit; ++j) {
11-
if (((x >> j) & 1) == 0) t |= (1 << j);
9+
vector<int> ans(n);
10+
for (int i = 0; i < n; ++i) {
11+
int x = nums[n - i - 1];
12+
int k = 0;
13+
for (int j = maximumBit - 1; ~j; --j) {
14+
if ((xs >> j & 1) == 0) {
15+
k |= 1 << j;
16+
}
1217
}
13-
ans.push_back(t);
18+
ans[i] = k;
19+
xs ^= x;
1420
}
1521
return ans;
1622
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
func getMaximumXor(nums []int, maximumBit int) []int {
2-
n := len(nums)
3-
s := make([]int, n+1)
4-
for i, v := range nums {
5-
s[i+1] = s[i] ^ v
1+
func getMaximumXor(nums []int, maximumBit int) (ans []int) {
2+
xs := 0
3+
for _, x := range nums {
4+
xs ^= x
65
}
7-
var ans []int
8-
for i := n; i > 0; i-- {
9-
t, x := 0, s[i]
10-
for j := 0; j < maximumBit; j++ {
11-
if ((x >> j) & 1) == 0 {
12-
t |= (1 << j)
6+
for i := range nums {
7+
x := nums[len(nums)-i-1]
8+
k := 0
9+
for j := maximumBit - 1; j >= 0; j-- {
10+
if xs>>j&1 == 0 {
11+
k |= 1 << j
1312
}
1413
}
15-
ans = append(ans, t)
14+
ans = append(ans, k)
15+
xs ^= x
1616
}
17-
return ans
17+
return
1818
}

0 commit comments

Comments
 (0)