File tree 1 file changed +11
-14
lines changed 1 file changed +11
-14
lines changed Original file line number Diff line number Diff line change @@ -32,36 +32,33 @@ class Solution {
32
32
class Solution2 {
33
33
public:
34
34
vector<int > maxHammingDistances (vector<int >& nums, int m) {
35
- vector<vector<int >> group (1 << m);
36
- for (int i = 0 ; i < size (nums); ++i) {
37
- group[((1 << m) - 1 ) ^ nums[i]].emplace_back (i);
38
- }
39
- vector<int > result (size (nums));
40
35
vector<int > q;
41
- vector<int > lookup (1 << m);
36
+ vector<int > dist (1 << m, - 1 );
42
37
for (const auto & x : nums) {
43
- if (lookup [x]) {
38
+ if (dist [x] != - 1 ) {
44
39
continue ;
45
40
}
46
- lookup [x] = true ;
41
+ dist [x] = 0 ;
47
42
q.emplace_back (x);
48
43
}
49
- for (int d = 0 ; !empty (q); ++d) {
44
+ for (int d = 0 ; !empty (q);) {
45
+ ++d;
50
46
vector<int > new_q;
51
47
for (const auto & u : q) {
52
- for (const auto & i : group[u]) {
53
- result[i] = m - d;
54
- }
55
48
for (int i = 0 ; i < m; ++i) {
56
- if (lookup [u ^ (1 << i)]) {
49
+ if (dist [u ^ (1 << i)] != - 1 ) {
57
50
continue ;
58
51
}
59
- lookup [u ^ (1 << i)] = true ;
52
+ dist [u ^ (1 << i)] = d ;
60
53
new_q.emplace_back (u ^ (1 << i));
61
54
}
62
55
}
63
56
q = move (new_q);
64
57
}
58
+ vector<int > result (size (nums));
59
+ for (int i = 0 ; i < size (nums); ++i) {
60
+ result[i] = m - dist[((1 << m) - 1 ) ^ nums[i]];
61
+ }
65
62
return result;
66
63
}
67
64
};
You can’t perform that action at this time.
0 commit comments