Skip to content

Commit 8260852

Browse files
authored
Create 1879.Minimum-XOR-Sum-of-Two-Arrays_v3.cpp
1 parent 1cc3539 commit 8260852

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
typedef pair<int,int> PII;
2+
3+
class Solution {
4+
public:
5+
int minimumXORSum(vector<int>& nums1, vector<int>& nums2)
6+
{
7+
int m = nums1.size();
8+
int n = nums2.size();
9+
vector<int>visited(1<<m, 0);
10+
priority_queue<PII, vector<PII>, greater<>>pq;
11+
pq.push({0,0});
12+
13+
while (!pq.empty())
14+
{
15+
auto [cost, state] = pq.top();
16+
pq.pop();
17+
18+
if (visited[state]) continue;
19+
visited[state] = 1;
20+
21+
int j = __builtin_popcount(state);
22+
if (j==n) return cost;
23+
24+
for (int i=0; i<m; i++)
25+
{
26+
if ((state>>i)&1) continue;
27+
int newState = state+(1<<i);
28+
if (visited[newState]) continue;
29+
pq.push({cost+(nums1[i]^nums2[j]), newState});
30+
}
31+
}
32+
33+
return -1;
34+
}
35+
};

0 commit comments

Comments
 (0)