Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 2 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Aug 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,8 @@
[1858.Longest-Word-With-All-Prefixes](https://github.com/wisdompeak/LeetCode/tree/master/Trie/1858.Longest-Word-With-All-Prefixes) (M)
[2416.Sum-of-Prefix-Scores-of-Strings](https://github.com/wisdompeak/LeetCode/tree/master/Trie/2416.Sum-of-Prefix-Scores-of-Strings) (M)
[2977.Minimum-Cost-to-Convert-String-II](https://github.com/wisdompeak/LeetCode/tree/master/Trie/2977.Minimum-Cost-to-Convert-String-II) (H)
[3093.Longest-Common-Suffix-Queries](https://github.com/wisdompeak/LeetCode/tree/master/Trie/3093.Longest-Common-Suffix-Queries) (H-)
[3093.Longest-Common-Suffix-Queries](https://github.com/wisdompeak/LeetCode/tree/master/Trie/3093.Longest-Common-Suffix-Queries) (H-)
[3670.Maximum-Product-of-Two-Integers-With-No-Common-Bits](https://github.com/wisdompeak/LeetCode/tree/master/Trie/3670.Maximum-Product-of-Two-Integers-With-No-Common-Bits) (H-)
* ``Trie and XOR``
[421.Maximum-XOR-of-Two-Numbers-in-an-Array](https://github.com/wisdompeak/LeetCode/tree/master/Trie/421.Maximum-XOR-of-Two-Numbers-in-an-Array) (H-)
[1707.Maximum-XOR-With-an-Element-From-Array](https://github.com/wisdompeak/LeetCode/tree/master/Trie/1707.Maximum-XOR-With-an-Element-From-Array) (H-)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class TrieNode {
public:
TrieNode* next[2];
TrieNode()
{
for (int i=0; i<2; i++)
next[i]=NULL;
}
};

class Solution {
TrieNode* root;
public:
void add(int x) {
TrieNode* node = root;
for (int i=0; i<31; i++) {
int b = ((x>>(30-i))&1);
if (!node->next[b])
node->next[b] = new TrieNode();
node = node->next[b];
}
}

int dfs(TrieNode* node, int i, int x) {
if (node==NULL) {
return -1;
}
if (i==31) return 0;

int b = ((x>>(30-i))&1);

if (b==1) {
int ans = dfs(node->next[0], i+1, x);
if (ans!=-1) return ans;
else return -1;
}
else {
int ans1 = dfs(node->next[1], i+1, x);
int ans2 = dfs(node->next[0], i+1, x);
if (ans1!=-1)
return (1<<(30-i))+ans1;
if (ans2!=-1)
return ans2;
return -1;
}
}

long long maxProduct(vector<int>& nums) {
root = new TrieNode();
int n = nums.size();

long long ret = 0;
for (int x: nums) {
int ans = dfs(root, 0, x);
if (ans!=-1) ret = max(ret, (long long)x*ans);
add(x);
}

return ret;
}
};

AltStyle によって変換されたページ (->オリジナル) /