-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaily144.cpp
71 lines (56 loc) · 1.58 KB
/
daily144.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Solution 1 - FAIL
class Solution {
public:
vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
/*
count set bits of all numbers?
find max bit = 2 ^ maxBit
store max index after every query
*/
int max_bit = std::pow(2, maximumBit);
int idx = nums.size() - 1;
auto xors = std::vector<int>(nums.size());
for (auto i = 0; i < nums.size(); ++i) {
auto val = nums[0];
for (auto j = 1; j <= idx; ++j) {
val ^= nums[j];
}
auto res = val ^ (max_bit - 1);
while (res >= max_bit) {
--res;
}
xors[i] = res;
--idx;
}
return xors;
}
};
// Solution 2
class Solution {
public:
vector<int> getMaximumXor(vector<int>& nums, int maximumBit) {
/*
count set bits of all numbers?
find max bit = 2 ^ maxBit
store max index after every query
*/
int max_bit = std::pow(2, maximumBit);
int idx = nums.size() - 1;
auto xors = std::vector<int>(nums.size());
auto val = nums[0];
for (auto j = 1; j < nums.size(); ++j) {
val ^= nums[j];
}
for (int i = nums.size() - 1; i >= 0; --i) {
if (i != nums.size() - 1)
val ^= nums[i + 1];
auto res = val ^ (max_bit - 1);
while (res >= max_bit) {
--res;
}
xors[nums.size() - 1 - i] = res;
--idx;
}
return xors;
}
};