id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n getHeight(root);\n return getInfectedTime(root, start, 0);\n }\n\nprivate:\n std::unordered_map<int, int> leftHeight;\n std::unordered_map<int, int> rightHeight;\n\n void getHeight(TreeNode* root) {\n if (!root) return;\n getHeight(root->left);\n getHeight(root->right);\n if (root->left) leftHeight[root->val] = max(leftHeight[root->left->val], rightHeight[root->left->val]) + 1;\n if (root->right) rightHeight[root->val] = max(leftHeight[root->right->val], rightHeight[root->right->val]) + 1;\n }\n\n int getInfectedTime(TreeNode* root, int start, int acc) {\n if (!root) {\n return -1;\n }\n if (root->val == start) {\n return max(acc, max(leftHeight[start], rightHeight[start]));\n }\n return max(getInfectedTime(root->left, start, max(rightHeight[root->val], acc) + 1),\n getInfectedTime(root->right, start, max(leftHeight[root->val], acc) + 1));\n }\n};\n",
"memory": "219900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n getHeight(root);\n return getInfectedTime(root, start, 0);\n }\n\nprivate:\n std::map<int, int> leftHeight;\n std::map<int, int> rightHeight;\n\n void getHeight(TreeNode* root) {\n if (!root) return;\n getHeight(root->left);\n getHeight(root->right);\n if (root->left) leftHeight[root->val] = max(leftHeight[root->left->val], rightHeight[root->left->val]) + 1;\n if (root->right) rightHeight[root->val] = max(leftHeight[root->right->val], rightHeight[root->right->val]) + 1;\n }\n\n int getInfectedTime(TreeNode* root, int start, int acc) {\n if (!root) {\n return -1;\n }\n if (root->val == start) {\n return max(acc, max(leftHeight[start], rightHeight[start]));\n }\n return max(getInfectedTime(root->left, start, max(rightHeight[root->val], acc) + 1),\n getInfectedTime(root->right, start, max(leftHeight[root->val], acc) + 1));\n }\n};\n",
"memory": "222700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<TreeNode*,TreeNode*>parent;\n map<TreeNode*,bool>visited;\n void mappingparent(TreeNode* root){\n if(root==NULL){\n return;\n }\n visited[root]=false;\n if(root->left){\n parent[root->left]=root;\n mappingparent(root->left);\n }\n if(root->right){\n parent[root->right]=root;\n mappingparent(root->right);\n }\n }\n TreeNode* findingnode(int start,TreeNode* root){\n if(!root){\n return NULL;\n }\n if(root->val==start){\n return root;\n }\n TreeNode*left=findingnode(start,root->left);\n TreeNode*right=findingnode(start,root->right);\n return (right==NULL)?left:right;\n }\n int amountOfTime(TreeNode* root, int start) {\n mappingparent(root);\n TreeNode*initial=findingnode(start,root);\n queue<TreeNode*>q;\n q.push(initial);\n visited[initial]=true;\n int ans=0;\n while(!q.empty()){\n int size=q.size();\n ans++;\n for(int i=0;i<size;i++){\n TreeNode* temp=q.front();\n q.pop();\n visited[temp]=true;\n if(temp->left&&!visited[temp->left]){\n q.push(temp->left);\n }\n if(temp->right&&!visited[temp->right]){\n q.push(temp->right);\n }\n if(parent[temp]&&!visited[parent[temp]]){\n q.push(parent[temp]);\n }\n }\n }\n return ans-1;\n }\n};",
"memory": "225500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int target) {\n int ans=INT_MIN;\n if(root==NULL){\n return 0;\n }\n map<int,vector<int>>m;\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty()){\n int sz=q.size();\n for(int i=0;i<sz;i++){\n TreeNode*a=q.front();\n q.pop();\n if(a->left!=NULL){\n q.push(a->left);\n m[a->val].push_back(a->left->val);\n m[a->left->val].push_back(a->val);\n }if(a->right!=NULL){\n q.push(a->right);\n m[a->val].push_back(a->right->val);\n m[a->right->val].push_back(a->val);\n }\n }\n \n }\n map<int,int>dis;\n for(auto it:m){\n if(it.first!=target){\n dis[it.first]=INT_MAX;\n }else{\n dis[target]=0;\n }\n }\n set<pair<int,int>>s; //dis,node\n s.insert({0,target});\n while(!s.empty()){\n auto u=*s.begin();\n s.erase(u);\n int dist=u.first;\n int node=u.second;\n for(auto it:m[node]){\n if(dis[it]>1+dis[node]){\n dis[it]=1+dis[node];\n s.insert({dis[it],it});\n }\n }\n \n }\n for(auto it:dis){\n ans=max(ans,it.second);\n }\n if(ans==INT_MIN){\n return 0;\n }\n return ans;\n }\n};",
"memory": "225500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void fill(vector<int> adj[], TreeNode *root){\n int lval = -1, rval = -1;\n if(root->left) lval = root->left->val;\n if(root->right) rval = root->right->val;\n int val = root->val;\n if(lval != -1){\n adj[lval].push_back(val);\n adj[val].push_back(lval);\n fill(adj, root->left);\n }\n if(rval != -1){\n adj[rval].push_back(val);\n adj[val].push_back(rval);\n fill(adj, root->right);\n }\n }\n\n int findT(int node, int par, vector<int> adj[]){\n int val = 0;\n for(auto &child: adj[node]){\n if(child == par) continue;\n val = max(findT(child, node, adj) + 1, val);\n }\n return val;\n }\n\n int amountOfTime(TreeNode* root, int start) {\n int MXSZ = 1e5+1;\n vector<int> adj[MXSZ];\n fill(adj, root);\n return findT(start, -1, adj);\n }\n};",
"memory": "228300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate:\n void getstart(TreeNode* root,int start,map<TreeNode*,TreeNode*> &parent,TreeNode* p,TreeNode* &s){\n if(!root) return;\n if(root->val == start) s = root;\n parent[root] = p;\n getstart(root->left,start,parent,root,s);\n getstart(root->right,start,parent,root,s);\n }\n\nint burnTree(TreeNode* root, map<TreeNode*, TreeNode*> parent) {\n if (!root) return 0; // Handle edge case where root is NULL\n \n map<TreeNode*, bool> visited;\n int count = 0;\n queue<TreeNode*> q;\n \n q.push(root);\n visited[root] = true;\n q.push(NULL); // Marker for the end of the level\n \n while (!q.empty()) {\n TreeNode* node = q.front();\n q.pop();\n \n if (!node) {\n // End of current level\n if (!q.empty()) {\n q.push(NULL); // Marker for the next level\n count++;\n }\n } else {\n // Mark the node as visited\n \n // Add children and parent to the queue if they are not visited\n if (node->left && !visited[node->left]) {\n q.push(node->left);\n visited[node->left] = true;\n }\n if (node->right && !visited[node->right]) {\n q.push(node->right);\n visited[node->right] = true;\n }\n if (parent[node] && !visited[parent[node]]) {\n q.push(parent[node]);\n visited[parent[node]] = true;\n }\n }\n }\n \n return count;\n}\n\npublic:\n int amountOfTime(TreeNode* root, int start) {\n map<TreeNode*,TreeNode*> parent;\n TreeNode* startNode = NULL;\n getstart(root,start,parent,NULL,startNode);\n return burnTree(startNode,parent);\n }\n};",
"memory": "231100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int start = 0;\n TreeNode* infected;\n unordered_map<TreeNode*, TreeNode*> parent;\n unordered_set<int> visited;\n \n TreeNode* postOrder(TreeNode* root) {\n if (root == nullptr)\n return root;\n \n TreeNode* left = postOrder(root->left);\n TreeNode* right = postOrder(root->right);\n\n if (left) {\n parent[left] = root;\n // cout<<left->val<<\"-->\"<<root->val<<endl;\n }\n if (right) {\n // cout<<right->val<<\"-->\"<<root->val<<endl;\n parent[right] = root;\n }\n if (root->val == start)\n infected = root;\n return root;\n }\n \n int maxDepth(TreeNode* root) {\n if (root == nullptr or (visited.find(root->val) != visited.end()))\n return 0;\n visited.insert(root->val);\n int left = maxDepth(root->left);\n int right = maxDepth(root->right);\n int par = maxDepth(parent[root]);\n return 1 + max(left, max(right, par));\n } \n \n int amountOfTime(TreeNode* root, int start) {\n // Recursive DFS\n // Time: O(n[parent pointer]+n[maxDepth]) ~ O(n)\n // Space: O(n[parent]+h[stack]+n[visited]) ~ O(n+h)\n \n this->start = start;\n postOrder(root);\n return maxDepth(infected)-1;\n }\n};",
"memory": "231100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n using int2=pair<int, int>;\n vector<int> adj[100001];\n void build_adj(TreeNode* root){\n if (!root) return;\n int x=root->val;\n if (root->left){\n int l=root->left->val;\n adj[x].push_back(l);\n adj[l].push_back(x);\n build_adj(root->left);\n }\n if (root->right){\n int r=root->right->val;\n adj[x].push_back(r);\n adj[r].push_back(x);\n build_adj(root->right);\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n fill(adj, adj+100001, vector<int>());\n build_adj(root);\n bool viz[100001]={0};\n queue<int2> q;\n q.emplace(start, 0);\n viz[start]=1;\n int d=0;\n while(!q.empty()){\n auto [x, dd]=q.front();\n d=max(d, dd);\n q.pop();\n for(int y: adj[x]){\n if (viz[y]) continue;\n q.emplace(y, dd+1);\n viz[y]=1;\n }\n }\n return d;\n }\n};\n\nauto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();",
"memory": "233900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n using int2=pair<int, int>;\n vector<int> adj[100001];\n void build_adj(TreeNode* root){\n if (!root) return;\n int x=root->val;\n if (root->left){\n int l=root->left->val;\n adj[x].push_back(l);\n adj[l].push_back(x);\n build_adj(root->left);\n }\n if (root->right){\n int r=root->right->val;\n adj[x].push_back(r);\n adj[r].push_back(x);\n build_adj(root->right);\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n fill(adj, adj+100001, vector<int>());\n build_adj(root);\n bool viz[100001]={0};\n queue<int2> q;\n q.emplace(start, 0);\n viz[start]=1;\n int d=0;\n while(!q.empty()){\n auto [x, dd]=q.front();\n d=max(d, dd);\n q.pop();\n for(int y: adj[x]){\n if (viz[y]) continue;\n q.emplace(y, dd+1);\n viz[y]=1;\n }\n }\n return d;\n }\n};\n\n",
"memory": "233900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // Helper function to find the maximum value in the binary tree\n int findMaxValue(TreeNode* root) {\n if (root == nullptr) return 0; // base case\n\n int leftMax = findMaxValue(root->left);\n int rightMax = findMaxValue(root->right);\n\n return max(root->val, max(leftMax, rightMax)); // return maximum value\n }\n\n // Function to create adjacency list from binary tree\n void createAdj(vector<vector<int>>& adj, TreeNode* root) {\n if (root == nullptr) return; // base case: root is null\n\n int node = root->val;\n if (root->left) {\n int left = root->left->val;\n adj[node].push_back(left);\n adj[left].push_back(node);\n createAdj(adj, root->left); // recursive call for left subtree\n }\n if (root->right) {\n int right = root->right->val;\n adj[node].push_back(right);\n adj[right].push_back(node);\n createAdj(adj, root->right); // recursive call for right subtree\n }\n }\n\n int amountOfTime(TreeNode* root, int start) {\n int ans=0;\n if (root == nullptr) return 0; // no nodes to infect\n\n // Step 1: Find the maximum node value to determine the size of the adjacency list\n int maxVal = findMaxValue(root);\n\n // Step 2: Initialize adjacency list, visited array, and distance array\n vector<vector<int>> adj(maxVal + 1); // Initialize adjacency list of size maxVal + 1\n vector<int> dist(maxVal + 1, INT_MIN); // Distance from the start node\n vector<bool> visited(maxVal + 1, false); // Visited array for BFS\n\n // Step 3: Create adjacency list from binary tree\n createAdj(adj, root);\n\n // Step 4: Perform BFS to calculate the maximum time to infect all nodes\n queue<int> q;\n q.push(start);\n visited[start] = true;\n dist[start] = 0;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n for (auto neighbor : adj[node]) {\n if (!visited[neighbor]) {\n visited[neighbor] = true;\n dist[neighbor] = dist[node] + 1;\n if(dist[neighbor]>ans)\n {\n ans=dist[neighbor];\n }\n q.push(neighbor);\n }\n }\n }\n\n // Step 5: Find the maximum distance, which is the time needed to infect all nodes\n return *max_element(dist.begin(), dist.end());\n }\n};\n",
"memory": "236700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // Helper function to find the maximum value in the binary tree\n int findMaxValue(TreeNode* root) {\n if (root == nullptr) return 0; // base case\n\n int leftMax = findMaxValue(root->left);\n int rightMax = findMaxValue(root->right);\n\n return max(root->val, max(leftMax, rightMax)); // return maximum value\n }\n\n // Function to create adjacency list from binary tree\n void createAdj(vector<vector<int>>& adj, TreeNode* root) {\n if (root == nullptr) return; // base case: root is null\n\n int node = root->val;\n if (root->left) {\n int left = root->left->val;\n adj[node].push_back(left);\n adj[left].push_back(node);\n createAdj(adj, root->left); // recursive call for left subtree\n }\n if (root->right) {\n int right = root->right->val;\n adj[node].push_back(right);\n adj[right].push_back(node);\n createAdj(adj, root->right); // recursive call for right subtree\n }\n }\n\n int amountOfTime(TreeNode* root, int start) {\n int ans=0;\n if (root == nullptr) return 0; // no nodes to infect\n\n // Step 1: Find the maximum node value to determine the size of the adjacency list\n int maxVal = findMaxValue(root);\n\n // Step 2: Initialize adjacency list, visited array, and distance array\n vector<vector<int>> adj(maxVal + 1); // Initialize adjacency list of size maxVal + 1\n vector<int> dist(maxVal + 1, INT_MIN); // Distance from the start node\n vector<bool> visited(maxVal + 1, false); // Visited array for BFS\n\n // Step 3: Create adjacency list from binary tree\n createAdj(adj, root);\n\n // Step 4: Perform BFS to calculate the maximum time to infect all nodes\n queue<int> q;\n q.push(start);\n visited[start] = true;\n dist[start] = 0;\n\n while (!q.empty()) {\n int node = q.front();\n q.pop();\n for (auto neighbor : adj[node]) {\n if (!visited[neighbor]) {\n visited[neighbor] = true;\n dist[neighbor] = dist[node] + 1;\n \n q.push(neighbor);\n }\n }\n }\n\n // Step 5: Find the maximum distance, which is the time needed to infect all nodes\n return *max_element(dist.begin(), dist.end());\n }\n};\n",
"memory": "236700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<long long>adj[100001];\n \n void helper(TreeNode* root)\n {\n if(!root)\n return;\n if(root->left)\n {\n adj[root->val].push_back(root->left->val);\n adj[root->left->val].push_back(root->val);\n \n }\n if(root->right)\n {\n adj[root->val].push_back(root->right->val);\n adj[root->right->val].push_back(root->val);\n \n }\n helper(root->left);\n helper(root->right);\n \n }\n int amountOfTime(TreeNode* root, int start) {\n vector<bool>vis(100001);\n helper(root);\n int time=0;\n queue<int>q;\n q.push(start);\n vis[start]=true;\n while(!q.empty())\n {\n int n=q.size();\n time++;\n while(n--)\n {\n int curr=q.front();\n q.pop();\n for(auto x:adj[curr])\n {\n if(!vis[x])\n {\n q.push(x);\n vis[x]=true;\n }\n }\n }\n }\n return time-1;\n }\n};",
"memory": "239500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<long long>adj[100001];\n \n void helper(TreeNode* root)\n {\n if(!root)\n return;\n if(root->left)\n {\n adj[root->val].push_back(root->left->val);\n adj[root->left->val].push_back(root->val);\n helper(root->left);\n }\n if(root->right)\n {\n adj[root->val].push_back(root->right->val);\n adj[root->right->val].push_back(root->val);\n helper(root->right);\n }\n \n \n }\n int amountOfTime(TreeNode* root, int start) {\n vector<bool>vis(100001);\n helper(root);\n int time=0;\n queue<int>q;\n q.push(start);\n vis[start]=true;\n while(!q.empty())\n {\n int n=q.size();\n time++;\n while(n--)\n {\n int curr=q.front();\n q.pop();\n for(auto x:adj[curr])\n {\n if(!vis[x])\n {\n q.push(x);\n vis[x]=true;\n }\n }\n }\n }\n return time-1;\n }\n};",
"memory": "242300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<long long>adj[100001];\n \n void helper(TreeNode* root)\n {\n if(!root)\n return;\n if(root->left)\n {\n adj[root->val].push_back(root->left->val);\n adj[root->left->val].push_back(root->val);\n \n }\n if(root->right)\n {\n adj[root->val].push_back(root->right->val);\n adj[root->right->val].push_back(root->val);\n \n }\n helper(root->left);\n helper(root->right);\n \n }\n int amountOfTime(TreeNode* root, int start) {\n vector<bool>vis(100001);\n helper(root);\n int time=0;\n queue<pair<int,int>>q;\n q.push({0,start});\n vis[start]=true;\n while(!q.empty())\n {\n int currTime=q.front().first;\n int currNode=q.front().second;\n q.pop();\n time=max(time,currTime);\n for(auto x:adj[currNode])\n {\n if(!vis[x])\n {\n q.push({currTime+1,x});\n vis[x]=true;\n }\n }\n \n }\n return time;\n \n }\n};",
"memory": "245100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\n#define Node TreeNode\nclass Solution {\npublic:\n int check(Node* root,int target,int k,int &ans){\n if(root == NULL) return -1;\n if(root->val == target){\n int curDis = 0;\n queue<Node*> que;\n que.push(root);\n while(!que.empty()){\n int size = que.size();\n for(int i = 0; i < size; i++){\n Node* node = que.front();\n que.pop();\n if(node->left)\n que.push(node->left);\n if(node->right)\n que.push(node->right);\n }\n curDis++;\n }\n ans = max(ans,(curDis-1));\n return 1;\n }\n int left = check(root->left,target,k,ans);\n if(left != -1){\n int curDis = left;\n // cout<<root->data<<curDis<<endl;\n queue<Node*> que;\n if(root->right != NULL)\n que.push(root->right);\n // curDis++;\n while(!que.empty()){\n int size = que.size();\n for(int i = 0; i < size; i++){\n Node* node = que.front();\n que.pop();\n if(node->left)\n que.push(node->left);\n if(node->right)\n que.push(node->right);\n }\n curDis++;\n }\n ans = max(ans,curDis);\n return left+1;\n }\n int right = check(root->right,target,k,ans);\n if(right != -1){\n int curDis = right;\n // cout<<root->data<<curDis<<endl;\n queue<Node*> que;\n if(root->left != NULL)\n que.push(root->left);\n // curDis++;\n while(!que.empty()){\n int size = que.size();\n for(int i = 0; i < size; i++){\n Node* node = que.front();\n que.pop();\n if(node->left)\n que.push(node->left);\n if(node->right)\n que.push(node->right);\n }\n curDis++;\n }\n ans = max(ans,curDis);\n return right+1;\n }\n return -1;\n }\n int amountOfTime(TreeNode* root, int start) {\n if(root == NULL) return 0;\n if(root->left == NULL && root->right == NULL) return 0; \n int ans = INT_MIN;\n check(root,start,0,ans);\n return ans;\n }\n};",
"memory": "245100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate: \n void getMyParent(TreeNode* root, map<int,vector<int>>& m1){\n if(root==NULL)return;\n if(root->left!=NULL)\n m1[root->left->val].push_back(root->val);\n if(root->right!=NULL)\n m1[root->right->val].push_back(root->val);\n getMyParent(root->left,m1);\n getMyParent(root->right,m1);\n }\n void getMyChildren(TreeNode* root, map<int,vector<int>>& m1){\n if(root==NULL)return;\n if(root->left!=NULL)\n m1[root->val].push_back(root->left->val);\n getMyChildren(root->left,m1);\n if(root->right!=NULL)\n m1[root->val].push_back(root->right->val);\n getMyChildren(root->right,m1);\n }\n int bfs_from_start_node(TreeNode* root,map<int,vector<int>> m1,int x){\n set<int> vis;\n int ans=0;\n queue<int> q;\n q.push(x);\n vis.insert(x);\n while(!q.empty()){\n int n=q.size();\n int f=0;\n while(n--){\n int jks = q.front();\n q.pop();\n for(auto it:m1[jks]){\n if(!vis.count(it)){\n q.push(it);\n f++;\n if(f==1)\n ans++;\n }\n vis.insert(it); \n }\n } \n }\n return ans;\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n ios_base::sync_with_stdio(false);\n\tcin.tie(NULL);\n map<int,vector<int>> m1; //populated with child-parent pairs\n getMyParent(root,m1);\n getMyChildren(root,m1);\n // bfs_from_start_node(root,m1);\n return bfs_from_start_node(root,m1,start);\n }\n};",
"memory": "247900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate: \n void getMyParent(TreeNode* root, map<int,vector<int>>& m1){\n if(root==NULL)return;\n if(root->left!=NULL)\n m1[root->left->val].push_back(root->val);\n if(root->right!=NULL)\n m1[root->right->val].push_back(root->val);\n getMyParent(root->left,m1);\n getMyParent(root->right,m1);\n }\n void getMyChildren(TreeNode* root, map<int,vector<int>>& m1){\n if(root==NULL)return;\n if(root->left!=NULL)\n m1[root->val].push_back(root->left->val);\n getMyChildren(root->left,m1);\n if(root->right!=NULL)\n m1[root->val].push_back(root->right->val);\n getMyChildren(root->right,m1);\n }\n int bfs_from_start_node(TreeNode* root,map<int,vector<int>> m1,int x){\n set<int> vis;\n int ans=0;\n queue<int> q;\n q.push(x);\n vis.insert(x);\n while(!q.empty()){\n int n=q.size();\n int f=0;\n while(n--){\n int jks = q.front();\n q.pop();\n for(auto it:m1[jks]){\n if(!vis.count(it)){\n q.push(it);\n f++;\n if(f==1)\n ans++;\n }\n vis.insert(it); \n }\n } \n }\n return ans;\n }\npublic:\n int amountOfTime(TreeNode* root, int start) {\n map<int,vector<int>> m1; //populated with child-parent pairs\n getMyParent(root,m1);\n getMyChildren(root,m1);\n // bfs_from_start_node(root,m1);\n return bfs_from_start_node(root,m1,start);\n }\n};",
"memory": "250700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\nprivate: \n void getMyParent(TreeNode* root, map<int,vector<int>>& m1){\n if(root==NULL)return;\n if(root->left!=NULL)\n m1[root->left->val].push_back(root->val);\n if(root->right!=NULL)\n m1[root->right->val].push_back(root->val);\n getMyParent(root->left,m1);\n getMyParent(root->right,m1);\n }\n void getMyChildren(TreeNode* root, map<int,vector<int>>& m1){\n if(root==NULL)return;\n if(root->left!=NULL)\n m1[root->val].push_back(root->left->val);\n getMyChildren(root->left,m1);\n if(root->right!=NULL)\n m1[root->val].push_back(root->right->val);\n getMyChildren(root->right,m1);\n }\n int bfs_from_start_node(TreeNode* root,map<int,vector<int>> m1,int x){\n set<int> vis;\n int ans=0;\n queue<int> q;\n q.push(x);\n vis.insert(x);\n while(!q.empty()){\n int n=q.size();\n int f=0;\n while(n--){\n int jks = q.front();\n q.pop();\n for(auto it:m1[jks]){\n if(!vis.count(it)){\n q.push(it);\n f++;\n if(f==1)\n ans++;\n }\n vis.insert(it);\n \n }\n }\n \n }\n return ans;\n }\n\n\npublic:\n int amountOfTime(TreeNode* root, int start) {\n map<int,vector<int>> m1; //populated with child-parent pairs\n getMyParent(root,m1);\n getMyChildren(root,m1);\n // bfs_from_start_node(root,m1);\n return bfs_from_start_node(root,m1,start);\n }\n};",
"memory": "253500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void tree2Graph(TreeNode* root, std::unordered_map<int, std::unordered_set<int>>& graph) {\n std::queue<TreeNode*> node_queue;\n node_queue.push(root);\n while (!node_queue.empty()) {\n TreeNode* node = node_queue.front();\n node_queue.pop();\n\n if (node->left) {\n graph[node->val].insert(node->left->val);\n graph[node->left->val].insert(node->val);\n node_queue.push(node->left);\n }\n if (node->right) {\n graph[node->val].insert(node->right->val);\n graph[node->right->val].insert(node->val);\n node_queue.push(node->right);\n }\n } \n }\n\n int calculateDistance(std::unordered_map<int, std::unordered_set<int>>& graph, int start) {\n std::queue<int> val_queue;\n std::unordered_set<int> visited;\n val_queue.push(start);\n visited.insert(start);\n int distance = 0;\n while (!val_queue.empty()) {\n int levelCount = val_queue.size();\n for (int i = 0; i < levelCount; ++i) {\n int val = val_queue.front();\n val_queue.pop();\n\n for (int neighbor : graph[val]) {\n if (visited.find(neighbor) == visited.end()) {\n visited.insert(neighbor);\n val_queue.push(neighbor);\n }\n }\n }\n ++distance;\n }\n return distance - 1;\n }\n\n int amountOfTime(TreeNode* root, int start) {\n std::unordered_map<int, std::unordered_set<int>> graph;\n tree2Graph(root, graph);\n return calculateDistance(graph, start);\n }\n};\n",
"memory": "267500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n \n unordered_map<TreeNode*,unordered_set<TreeNode*>>mp;\n\n queue<TreeNode*>qa;\n qa.push(root);\n\n TreeNode* s;\n\n while(qa.size()!=0)\n {\n int sz = qa.size();\n\n for(int i=0;i<sz;i++)\n {\n TreeNode* x = qa.front();\n qa.pop();\n\n if(x->val==start)\n {\n s = x;\n }\n\n if(x->left!=NULL)\n {\n mp[x].insert(x->left);\n mp[x->left].insert(x);\n\n qa.push(x->left);\n }\n\n if(x->right!=NULL)\n {\n mp[x].insert(x->right);\n mp[x->right].insert(x);\n\n qa.push(x->right);\n }\n }\n }\n\n queue<TreeNode*>qb;\n qb.push(s);\n\n unordered_set<TreeNode*>vis;\n vis.insert(s);\n\n int curlvl=-1;\n\n while(qb.size()!=0)\n {\n curlvl++;\n\n int sz = qb.size();\n\n for(int i=0;i<sz;i++)\n {\n TreeNode* x = qb.front();\n qb.pop();\n\n for(auto adj:mp[x])\n {\n if(!vis.contains(adj))\n {\n qb.push(adj);\n vis.insert(adj);\n }\n }\n }\n }\n\n return curlvl;\n }\n};",
"memory": "270300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int creategraph(unordered_map<int, vector<int>>&graph, TreeNode *root)\n {\n if(root)\n {\n if(root->left){\n graph[root->val].push_back(root->left->val);\n graph[root->left->val].push_back(root->val);\n\n }\n if(root->right){\n graph[root->val].push_back(root->right->val);\n graph[root->right->val].push_back(root->val);\n }\n return max(root->val, max(creategraph(graph, root->left), creategraph(graph, root->right)));\n \n\n }\n return 0;\n }\n\n\n int amountOfTime(TreeNode* root, int start) {\n\n unordered_map<int, vector<int>>graph;\n\n int count=creategraph(graph, root);\n \n int time=-1;\n queue<int>q;\n vector<int>infected(count+1, 0);\n q.push(start);\n infected[start]=1;\n while(!q.empty())\n {\n int sz=q.size();\n time++;\n while(sz--)\n {\n int v=q.front();\n q.pop();\n for(auto &neigh:graph[v])\n {\n cout<<neigh<<\" \";\n if(!infected[neigh])\n {\n infected[neigh]=1;\n q.push(neigh);\n\n }\n }\n\n }\n }\n return time;\n \n }\n};",
"memory": "273100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int ans=0;\n int start=0;\n vector<int> dfs(TreeNode* root){\n if(root==NULL) return {0,0};\n auto a=dfs(root->left);\n auto b=dfs(root->right);\n\n if(root->val==start){\n ans=max(ans,max(a[1],b[1]));\n return {1,0};\n }\n\n if(a[0]==1){\n ans=max(ans,b[1]+a[1]+1);\n return {1,a[1]+1};\n }\n \n if(b[0]==1){\n ans=max(ans,b[1]+a[1]+1);\n return {1,b[1]+1};\n }\n return {0,1+max(a[1],b[1])};\n }\n int amountOfTime(TreeNode* root, int st) {\n start=st;\n dfs(root);\n return ans;\n }\n};",
"memory": "273100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int ans=0;\n int start=0;\n vector<int> dfs(TreeNode* root){\n if(root==NULL) return {0,0};\n auto a=dfs(root->left);\n auto b=dfs(root->right);\n\n if(root->val==start){\n ans=max(ans,max(a[1],b[1]));\n return {1,1};\n }\n\n if(a[0]==1){\n ans=max(ans,b[1]+a[1]);\n return {1,a[1]+1};\n }\n \n if(b[0]==1){\n ans=max(ans,b[1]+a[1]);\n return {1,b[1]+1};\n }\n return {0,1+max(a[1],b[1])};\n }\n int amountOfTime(TreeNode* root, int st) {\n start=st;\n dfs(root);\n return ans;\n }\n};",
"memory": "275900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int ans = 0;\n void graph(TreeNode* root , map<TreeNode * , vector<TreeNode*>> &mp)\n {\n if(!root) return ;\n else\n {\n if(root->left)\n {\n mp[root].push_back(root->left);\n mp[root->left].push_back(root);\n graph(root->left,mp);\n }\n\n if(root->right)\n {\n mp[root].push_back(root->right);\n mp[root->right].push_back(root);\n graph(root->right,mp);\n }\n }\n }\n\n void bfs(TreeNode * start , map<TreeNode * , vector<TreeNode*>> &mp , int time)\n {\n ans = max(ans,time);\n int og = start->val;\n start->val = 0;\n for(auto it : mp[start])\n {\n if(it->val>0)\n {\n time++;\n bfs(it,mp,time);\n time--;\n }\n }\n\n start->val = og;\n }\n \n TreeNode * find_node(int start , TreeNode * root)\n {\n if(!root) return NULL;\n if(start == root->val) return root;\n TreeNode * ans1 = find_node(start,root->left);\n TreeNode * ans2 = find_node(start,root->right);\n if(ans1) return ans1;\n return ans2;\n }\n\n int amountOfTime(TreeNode* root, int start) {\n map<TreeNode * , vector<TreeNode*>> mp;\n graph(root,mp);\n TreeNode * strt = find_node(start,root);\n int time = 0;\n bfs(strt,mp,time);\n return ans;\n }\n};",
"memory": "278700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n if (!root)\n return 0;\n \n unordered_map<int, vector<int>> g;\n \n function<void(TreeNode*)> buildGraph = [&](TreeNode* root) {\n if (!root)\n return;\n \n if (root->left) {\n g[root->val].push_back(root->left->val);\n g[root->left->val].push_back(root->val);\n }\n if (root->right) {\n g[root->val].push_back(root->right->val);\n g[root->right->val].push_back(root->val);\n }\n \n buildGraph(root->left);\n buildGraph(root->right);\n };\n \n function<int(int, int)> dfs = [&](int cur, int parent) -> int {\n int ans = 0;\n \n for (int c: g[cur]) {\n if (c == parent)\n continue;\n ans = max(ans, 1 + dfs(c, cur));\n }\n \n return ans;\n };\n \n buildGraph(root);\n \n \n return dfs(start, -1);\n }\n};",
"memory": "281500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int, vector<int>> adj;\n\n function<void(TreeNode*, TreeNode*)> dfs =\n [&](TreeNode* node, TreeNode* parent) -> void {\n if (!node) {\n return;\n }\n if (parent) {\n adj[node->val].push_back(parent->val);\n adj[parent->val].push_back(node->val);\n }\n dfs(node->left, node);\n dfs(node->right, node);\n };\n\n function<int(int, int)> dfs2 = [&](int node, int from) -> int {\n int result = 0;\n for (auto to : adj[node]) {\n if (to != from) {\n result = max(result, dfs2(to, node) + 1);\n }\n }\n return result;\n };\n \n dfs(root, NULL);\n return dfs2(start, -1);\n }\n};",
"memory": "284300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> solve(TreeNode* root, int start){\n if(root==NULL){\n return {0,0,0};\n }\n if(root->val==start){\n auto l=solve(root->left,start);\n auto r=solve(root->right,start);\n int y=max(l[0],r[0]);\n return {1,y,1};\n }\n auto l=solve(root->left,start);\n auto r=solve(root->right,start);\n if(l[2]==1){\n return {1+l[0],max({l[1],r[1],l[0]+r[0]}),1};\n }\n else if(r[2]==1){\n return {1+r[0],max({l[1],r[1],l[0]+r[0]}),1};\n }\n else{\n return {1+max(r[0],l[0]),max(l[1],r[1]),0};\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n auto ans=solve(root,start);\n return ans[1];\n }\n};",
"memory": "287100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> solve(TreeNode* root, int start){\n if(root==NULL){\n return {0,0,0};\n }\n if(root->val==start){\n auto l=solve(root->left,start);\n auto r=solve(root->right,start);\n int y=max(l[0],r[0]);\n return {1,y,1};\n }\n auto l=solve(root->left,start);\n auto r=solve(root->right,start);\n if(l[2]==1){\n return {1+l[0],max({l[1],r[1],l[0]+r[0]}),1};\n }\n else if(r[2]==1){\n return {1+r[0],max({l[1],r[1],l[0]+r[0]}),1};\n }\n else{\n return {1+max(r[0],l[0]),max(l[1],r[1]),0};\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n auto ans=solve(root,start);\n return ans[1];\n }\n};",
"memory": "287100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n \n std::map<int, std::vector<int>> nodes;\n\n auto dfs = [&nodes, &start](decltype(root) root, auto&& dfs){\n\n if(root == nullptr) return;\n auto key_itr = nodes.find(root->val);\n if(key_itr == nodes.end())\n nodes[root->val] = {};\n if(root->left){\n nodes[root->val].push_back(root->left->val);\n dfs(root->left, dfs);\n nodes[root->left->val].push_back(root->val);\n }\n if(root->right){\n nodes[root->val].push_back(root->right->val);\n dfs(root->right, dfs);\n nodes[root->right->val].push_back(root->val);\n }\n };\n\n dfs(root, dfs);\n\n // for(auto [_key, _node_list]: nodes)\n // {\n // std::cout << _key << \": \";\n // for(auto e: _node_list)\n // {\n // std::cout << e << \", \";\n // } \n // std::cout << '\\n';\n // }\n\n std::set<int> visited;\n std::deque<int> q;\n q.push_back(start);\n int time = -1;\n while(q.size()!=0)\n {\n auto n = q.size();\n time++;\n while(n-- > 0)\n {\n auto node = q[0];\n q.pop_front();\n if(visited.find(node) != visited.end()) continue;\n for(auto _node: nodes[node])\n {\n if(visited.find(_node) != visited.end()) continue;\n q.push_back(_node);\n }\n visited.insert(node);\n }\n }\n\n return time;\n };\n\n};\n\n\n/*\n\n2: 3\n3: 2, 4, 1\n4: 3 \n1: 3\n\n*/",
"memory": "289900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int amountOfTime(TreeNode* root, int start) {\n \n std::map<int, std::vector<int>> nodes;\n\n auto dfs = [&nodes](decltype(root) root, auto&& dfs){\n\n if(root == nullptr) return;\n auto key_itr = nodes.find(root->val);\n if(key_itr == nodes.end())\n nodes[root->val] = {};\n if(root->left){\n nodes[root->val].push_back(root->left->val);\n dfs(root->left, dfs);\n nodes[root->left->val].push_back(root->val);\n }\n if(root->right){\n nodes[root->val].push_back(root->right->val);\n dfs(root->right, dfs);\n nodes[root->right->val].push_back(root->val);\n }\n };\n\n dfs(root, dfs);\n\n // for(auto [_key, _node_list]: nodes)\n // {\n // std::cout << _key << \": \";\n // for(auto e: _node_list)\n // {\n // std::cout << e << \", \";\n // } \n // std::cout << '\\n';\n // }\n\n std::set<int> visited;\n std::deque<int> q;\n q.push_back(start);\n int time = -1;\n while(q.size()!=0)\n {\n auto n = q.size();\n time++;\n while(n-- > 0)\n {\n auto node = q[0];\n q.pop_front();\n if(visited.find(node) != visited.end()) continue;\n for(auto _node: nodes[node])\n {\n if(visited.find(_node) != visited.end()) continue;\n q.push_back(_node);\n }\n visited.insert(node);\n }\n }\n\n return time;\n };\n\n};\n\n\n/*\n\n2: 3\n3: 2, 4, 1\n4: 3 \n1: 3\n\n*/",
"memory": "292700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void GetTraversal(TreeNode* root,TreeNode* Parent,unordered_map<int,list<int>> &AdjList,unordered_map<int,bool> &visit){\n if(root==NULL){\n return;\n }\n visit[root->val]=false;\n if(Parent!=NULL){\n AdjList[root->val].push_back(Parent->val);\n }\n if(root->left != NULL){\n GetTraversal(root->left,root,AdjList,visit);\n AdjList[root->val].push_back(root->left->val);\n }\n if(root->right != NULL){\n GetTraversal(root->right,root,AdjList,visit);\n AdjList[root->val].push_back(root->right->val);\n }\n }\n int amountOfTime(TreeNode* root, int target) {\n unordered_map<int,list<int>> AdjList;\n TreeNode* Parent=NULL;\n unordered_map<int,bool> visit;\n GetTraversal(root,Parent,AdjList,visit);\n \n queue<pair<int,int>> q;\n q.push(make_pair(0,target));\n visit[target]=true;\n int MinTime=0;\n while(!q.empty()){\n pair<int,int> CurrentPair=q.front();\n q.pop();\n int CurrentTime=CurrentPair.first;\n int CurrentNode=CurrentPair.second;\n MinTime=CurrentTime;\n \n for(auto Neighbour:AdjList[CurrentNode]){\n if(!visit[Neighbour]){\n visit[Neighbour]=true;\n q.push({CurrentTime+1,Neighbour});\n }\n }\n }\n return MinTime;\n }\n};",
"memory": "295500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void GetTraversal(TreeNode* root,TreeNode* Parent,unordered_map<int,list<int>> &AdjList,unordered_map<int,bool> &visit){\n if(root==NULL){\n return;\n }\n visit[root->val]=false;\n if(Parent!=NULL){\n AdjList[root->val].push_back(Parent->val);\n }\n if(root->left != NULL){\n GetTraversal(root->left,root,AdjList,visit);\n AdjList[root->val].push_back(root->left->val);\n }\n if(root->right != NULL){\n GetTraversal(root->right,root,AdjList,visit);\n AdjList[root->val].push_back(root->right->val);\n }\n }\n int amountOfTime(TreeNode* root, int target) {\n unordered_map<int,list<int>> AdjList;\n TreeNode* Parent=NULL;\n unordered_map<int,bool> visit;\n GetTraversal(root,Parent,AdjList,visit);\n \n queue<pair<int,int>> q;\n q.push(make_pair(0,target));\n visit[target]=true;\n int MinTime=0;\n while(!q.empty()){\n pair<int,int> CurrentPair=q.front();\n q.pop();\n int CurrentTime=CurrentPair.first;\n int CurrentNode=CurrentPair.second;\n MinTime=CurrentTime;\n \n for(auto Neighbour:AdjList[CurrentNode]){\n if(!visit[Neighbour]){\n visit[Neighbour]=true;\n q.push({CurrentTime+1,Neighbour});\n }\n }\n }\n return MinTime;\n }\n};",
"memory": "298300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n vector<int>adj[1000001];\n map<int,int>mp;\n int count;\n void dfs(TreeNode* root){\n if(root == nullptr){\n return;\n }\n else{\n mp[root->val]= count;\n int fixed = count;\n if(root->left != nullptr){\n count++;\n adj[fixed].push_back(count);\n adj[count].push_back(fixed);\n dfs(root->left);\n }\n if(root->right != nullptr){\n count++;\n adj[fixed].push_back(count);\n adj[count].push_back(fixed);\n dfs(root->right);\n }\n }\n }\n\n\n int amountOfTime(TreeNode* root, int start) {\n count = 0;\n dfs(root);\n queue<int> q;\n vector<int> visited(count + 1 , 0);\n visited[mp[start]] = 1;\n q.push(mp[start]);\n vector<int>dist(count + 1 , INT_MAX);\n dist[mp[start]] = 0;\n while(!q.empty()){\n int node = q.front();\n q.pop();\n visited[node] = 1;\n for(auto v : adj[node]){\n if(visited[v]){\n continue;\n }\n else{\n if(dist[v] > dist[node] + 1){\n dist[v] = dist[node] + 1;\n q.push(v);\n }\n }\n }\n }\n sort(dist.begin() , dist.end());\n return dist[count];\n }\n};",
"memory": "301100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> adj[100001];\n void dfs(TreeNode* root,int par,set<int> &s)\n {\n s.insert(root->val);\n if(par!=-1)\n {\n adj[root->val].push_back(par);\n adj[par].push_back(root->val);\n }\n if(root->left!=NULL && s.find(root->left->val)==s.end())\n dfs(root->left,root->val,s);\n if(root->right!=NULL && s.find(root->right->val)==s.end())\n dfs(root->right,root->val,s);\n }\n int amountOfTime(TreeNode* root, int start) {\n set<int> s;\n dfs(root,-1,s);\n int minute=0;\n queue<int> q;\n q.push(start);\n set<int> vis;\n while(!q.empty())\n {\n int sz=q.size();\n while(sz--)\n {\n int temp=q.front();\n // vis.insert(temp);\n q.pop();\n vis.insert(temp);\n for(auto j:adj[temp])\n {\n if(vis.find(j)==vis.end())\n q.push(j);\n }\n }\n minute++;\n }\n return minute-1;\n }\n};",
"memory": "303900"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void make_graph(unordered_map<int, vector<int>> &adj, TreeNode* curr, int parent){\n if(curr == NULL) return;\n\n if(parent != -1){\n adj[curr -> val].push_back(parent);\n }\n\n if(curr -> left){\n adj[curr -> val].push_back(curr -> left -> val);\n make_graph(adj, curr -> left, curr -> val);\n }\n \n if(curr -> right){\n adj[curr -> val].push_back(curr -> right -> val);\n make_graph(adj, curr -> right, curr -> val);\n }\n\n }\n\n int amountOfTime(TreeNode* root, int start) {\n if(root == NULL) return 0;\n\n //Make adjacency list\n unordered_map<int, vector<int>>adj;\n make_graph(adj, root, -1);\n\n queue<int> q;\n q.push(start);\n unordered_map<int, bool> vis;\n vis[start] = true;\n int ans = 0;\n\n //BFS\n while( !q.empty() ){\n int size = q.size();\n\n while( size -- ){\n\n int temp = q.front();\n q.pop();\n\n for(auto &nbr : adj[temp]){\n if( !vis[nbr] ){\n q.push(nbr);\n vis[nbr] = true;\n }\n }\n }\n ans++;\n }\n\n return ans - 1;\n }\n};",
"memory": "306700"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nvoid undirectedTree(unordered_map<int,vector<int>>&adj,int parent,TreeNode* root){\n if(!root) return;\n if(parent!=-1){\n adj[root->val].push_back(parent);\n }\n\n if(root->left){\n adj[root->val].push_back(root->left->val);\n undirectedTree(adj, root->val, root->left);\n }\n\n if(root->right){\n adj[root->val].push_back(root->right->val);\n undirectedTree(adj, root->val, root->right);\n }\n}\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int,vector<int>>adj;\n undirectedTree(adj,-1,root);\n queue<int>q;\n unordered_set<int>vis;\n q.push(start);\n vis.insert(start);\n int min=0;\n while(!q.empty()){\n int l=q.size();\n for(int i=0;i<l;i++){\n int curr=q.front();\n q.pop();\n for(auto ngbr:adj[curr]){\n if(vis.find(ngbr)==vis.end()){\n q.push(ngbr);\n vis.insert(ngbr);\n }\n }\n }\n min++;\n }\n return min-1;\n }\n};",
"memory": "309500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void makeGraph(TreeNode* curr, int parent, unordered_map<int, vector<int>>& mp){\n if(!curr) return;\n \n if(parent != -1){\n mp[curr->val].push_back(parent);\n }\n if (curr->left) {\n mp[curr->val].push_back(curr->left->val);\n makeGraph(curr->left, curr->val, mp);\n }\n\n if (curr->right) {\n mp[curr->val].push_back(curr->right->val);\n makeGraph(curr->right, curr->val, mp);\n }\n }\n int amountOfTime(TreeNode* root, int start) {\n \n int minutes = 0;\n\n unordered_map<int, vector<int>> mp;\n makeGraph(root, -1, mp);\n\n queue<int> que;\n que.push(start);\n\n unordered_set<int> visited;\n visited.insert(start);\n\n while(!que.empty()){\n int n = que.size();\n bool foundNew = false; // Track if new nodes are discovered\n\n while (n--) {\n int curr = que.front();\n que.pop();\n\n for (int& ngbr : mp[curr]) {\n if (visited.find(ngbr) == visited.end()) {\n que.push(ngbr);\n visited.insert(ngbr);\n foundNew = true; // New node found\n }\n }\n }\n\n if (foundNew) {\n minutes++; // Increment only when new nodes are found\n }\n }\n\n return minutes;\n }\n};",
"memory": "309500"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n void make_graph(unordered_map<int, vector<int>> &adj, TreeNode* root, int parent) {\n if(root == NULL) return;\n\n if(parent != -1) {\n adj[root->val].push_back(parent);\n }\n\n if(root->left) {\n adj[root->val].push_back(root->left->val);\n make_graph(adj, root->left, root->val);\n }\n\n if(root->right) {\n adj[root->val].push_back(root->right->val);\n make_graph(adj, root->right, root->val);\n }\n }\n\n int amountOfTime(TreeNode* root, int start) {\n if(root == NULL) return 0;\n\n // Create adjacency list\n unordered_map<int, vector<int>> adj;\n make_graph(adj, root, -1);\n\n // BFS to calculate time to infect entire tree\n queue<int> q;\n unordered_map<int, bool> vis;\n q.push(start);\n vis[start] = true;\n int timeRequired = 0;\n\n while(!q.empty()) {\n int size = q.size();\n while(size--) {\n int node = q.front();\n q.pop();\n\n for(int &neighbor : adj[node]) {\n if(!vis[neighbor]) {\n q.push(neighbor);\n vis[neighbor] = true;\n }\n }\n }\n timeRequired++;\n }\n\n return timeRequired - 1;\n }\n};\n",
"memory": "312300"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n void make_list(TreeNode* root, int parent, unordered_map<int, vector<int>>& adj) {\n if (root == nullptr) {\n return;\n }\n if (parent != -1) {\n adj[root->val].push_back(parent);\n adj[parent].push_back(root->val);\n }\n if (root->left) {\n make_list(root->left, root->val, adj);\n }\n if (root->right) {\n make_list(root->right, root->val, adj);\n }\n }\n\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int, vector<int>> adj;\n unordered_set<int> visited;\n \n make_list(root, -1, adj);\n\n queue<int> q;\n q.push(start);\n visited.insert(start);\n int minTime = 0;\n\n while (!q.empty()) {\n int size = q.size();\n \n for (int i = 0; i < size; i++) {\n int front = q.front();\n q.pop();\n for (int neighbor : adj[front]) {\n if (visited.find(neighbor) == visited.end()) {\n q.push(neighbor);\n visited.insert(neighbor);\n }\n }\n }\n if (!q.empty()) { // Only increment time if there are still nodes to process\n minTime++;\n }\n }\n return minTime;\n }\n};\n",
"memory": "315100"
} |
2,461 | <p>You are given the <code>root</code> of a binary tree with <strong>unique</strong> values, and an integer <code>start</code>. At minute <code>0</code>, an <strong>infection</strong> starts from the node with value <code>start</code>.</p>
<p>Each minute, a node becomes infected if:</p>
<ul>
<li>The node is currently uninfected.</li>
<li>The node is adjacent to an infected node.</li>
</ul>
<p>Return <em>the number of minutes needed for the entire tree to be infected.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231744-1.png" style="width: 400px; height: 306px;" />
<pre>
<strong>Input:</strong> root = [1,5,3,null,4,10,6,9,2], start = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The following nodes are infected during:
- Minute 0: Node 3
- Minute 1: Nodes 1, 10 and 6
- Minute 2: Node 5
- Minute 3: Node 4
- Minute 4: Nodes 9 and 2
It takes 4 minutes for the whole tree to be infected so we return 4.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/25/image-20220625231812-2.png" style="width: 75px; height: 66px;" />
<pre>
<strong>Input:</strong> root = [1], start = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> At minute 0, the only node in the tree is infected so we return 0.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
<li>Each node has a <strong>unique</strong> value.</li>
<li>A node with a value of <code>start</code> exists in the tree.</li>
</ul>
| 3 | {
"code": "// /**\n// * Definition for a binary tree node.\n// * struct TreeNode {\n// * int val;\n// * TreeNode *left;\n// * TreeNode *right;\n// * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n// * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n// * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n// * };\n// */\n// class Solution {\n// public: \n// void makegraph(unordered_map<int,vector<int>> &adj, int parent , TreeNode* curr){\n// if(curr == NULL){\n// return;\n// }\n\n// if(parent != -1){\n// adj[curr->val].push_back(parent);\n// }\n\n// if(curr->left){\n// adj[curr->val].push_back(curr->left->val);\n// }\n\n// if(curr->right){\n// adj[curr->val].push_back(curr->right->val);\n// }\n\n// makegraph(adj, curr->val, curr->left);\n// makegraph(adj, curr->val, curr->right);\n// }\n\n// int amountOfTime(TreeNode* root, int start) {\n// unordered_map<int,vector<int>> adj;\n// makegraph(adj,-1,root);\n\n// unordered_set<int> visited;\n// queue<int> q;\n// q.push(start);\n// visited.insert(start);\n\n// int minutes = 0;\n// while(!q.empty()){\n// int n = q.size();\n\n// while(n--){\n// int node = q.front();\n// q.pop();\n\n// for(auto adjnode: adj[node]){\n// if(visited.find(adjnode) == visited.end()){\n// q.push(adjnode);\n// visited.insert(adjnode);\n// }\n// }\n// }\n\n// minutes++;\n// }\n\n// return minutes -1;\n// }\n// };\n\n#include <vector>\n#include <unordered_map>\n#include <unordered_set>\n#include <queue>\n\nusing namespace std;\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void makegraph(unordered_map<int, vector<int>> &adj, int parent, TreeNode* curr) {\n if (!curr) return;\n\n if (parent != -1) {\n adj[curr->val].push_back(parent);\n adj[parent].push_back(curr->val); // Ensure bidirectional connection\n }\n\n if (curr->left) {\n makegraph(adj, curr->val, curr->left);\n }\n\n if (curr->right) {\n makegraph(adj, curr->val, curr->right);\n }\n }\n\n int amountOfTime(TreeNode* root, int start) {\n unordered_map<int, vector<int>> adj;\n makegraph(adj, -1, root);\n\n unordered_set<int> visited;\n queue<int> q;\n q.push(start);\n visited.insert(start);\n\n int minutes = 0;\n\n while (!q.empty()) {\n int levelSize = q.size();\n for (int i = 0; i < levelSize; ++i) {\n int node = q.front();\n q.pop();\n\n for (int neighbor : adj[node]) {\n if (visited.find(neighbor) == visited.end()) {\n visited.insert(neighbor);\n q.push(neighbor);\n }\n }\n }\n ++minutes;\n }\n\n return minutes - 1; // Subtract one to account for the extra increment after the last level\n }\n};\n",
"memory": "315100"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int c=-1;\n for(int i=0;i<nums.size();i++){\n int n=(nums[i]*-1);\n if((find(nums.begin(),nums.end(),n)!=nums.end()) && n>c){\n c=n;\n }\n }\n return c;\n }\n};",
"memory": "22500"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int ans=-1;\n for(int i:nums)\n {\n for(int j:nums)\n {\n if(i==-j)\n {\n ans=max(ans,abs(i));\n \n }\n }\n }\n return ans; \n }\n};",
"memory": "22600"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int ans=-1;\n for(int i:nums){\n for(int j:nums){\n if(i == -j){\n ans=max(ans,abs(i));\n }\n }\n }\n return ans;\n }\n};",
"memory": "22700"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<bool> arr(2001);\n int mx = -1;\n for (auto i : nums){\n if (i > 0 and arr[-i+1001]) mx = max(mx, i);\n if (i < 0 and arr[-i+1001]) mx = max(mx, -i);\n arr[i+1001] = true;\n }\n return mx;\n }\n};",
"memory": "22800"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<bool> arr(2001);\n int mx = -1;\n for (auto i : nums){\n if (i > 0 and arr[-i+1001]) mx = max(mx, i);\n if (i < 0 and arr[-i+1001]) mx = max(mx, -i);\n arr[i+1001] = true;\n }\n return mx;\n }\n};",
"memory": "22800"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<bool> arr(2001);\n int mx = -1;\n for (auto i : nums){\n if (i > 0 and arr[-i+1001]) mx = max(mx, i);\n if (i < 0 and arr[-i+1001]) mx = max(mx, -i);\n arr[i+1001] = true;\n }\n return mx;\n }\n};",
"memory": "22900"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<bool> arr(2001);\n int mx = -1;\n for (auto i : nums){\n if (i > 0 and arr[-i+1001]) mx = max(mx, i);\n if (i < 0 and arr[-i+1001]) mx = max(mx, -i);\n arr[i+1001] = true;\n }\n return mx;\n }\n};",
"memory": "23000"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<bool> arr(2001);\n int mx = -1;\n for (auto i : nums){\n if (i > 0 and arr[-i+1001]) mx = max(mx, i);\n if (i < 0 and arr[-i+1001]) mx = max(mx, -i);\n arr[i+1001] = true;\n }\n return mx;\n }\n};",
"memory": "23000"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int n = nums.size(), ans = -1, idx;\n vector<int8_t> seen(1001, 0);\n\n for (int i = 0; i < n; i++) {\n idx = abs(nums[i]);\n if (!seen[idx])\n seen[idx] = nums[i] > 0? 1 : -1;\n else if (nums[i] * seen[idx] < 0)\n ans = max(ans, idx);\n }\n\n return ans;\n }\n};",
"memory": "23100"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int n = nums.size(), ans = -1, idx;\n vector<int8_t> seen(1001, 0);\n\n for (int i = 0; i < n; i++) {\n idx = abs(nums[i]);\n if (!seen[idx])\n seen[idx] = nums[i] > 0? 1 : -1;\n else if (nums[i] * seen[idx] < 0)\n ans = max(ans, idx);\n }\n\n return ans;\n }\n};",
"memory": "23100"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int n = nums.size(), ans = -1, idx;\n vector<int8_t> seen(1001, 0);\n\n for (int i = 0; i < n; i++) {\n idx = abs(nums[i]);\n if (!seen[idx])\n seen[idx] = nums[i] > 0? 1 : -1;\n else if (nums[i] * seen[idx] < 0)\n ans = max(ans, idx);\n }\n\n return ans;\n }\n};",
"memory": "23200"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int i=0;\n int j=nums.size()-1;\n while(i<j){\n if(nums[i]+nums[j]==0) return nums[j];\n else if(nums[i]+nums[j]<0) i++;\n else j--;\n } \n return -1;\n }\n};",
"memory": "23200"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int n = nums.size(), ans = -1, idx;\n vector<int8_t> seen(1001, 0);\n\n for (int i = 0; i < n; i++) {\n idx = abs(nums[i]);\n if (!seen[idx])\n seen[idx] = nums[i] > 0? 1 : -1;\n else if (nums[i] * seen[idx] < 0)\n ans = max(ans, idx);\n }\n\n return ans;\n }\n};",
"memory": "23300"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int i=0;\n int j=nums.size()-1;\n int maxi=-1;\n while(i<=j)\n {\n if(nums[i]==-nums[j])\n {\n maxi=max(maxi,nums[j]);\n i++;\n j--;\n }\n else if(nums[i]<-nums[j])\n i++;\n else\n j--;\n }\n return maxi;\n }\n};",
"memory": "23300"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int i = 0, j = nums.size() - 1;\n while (i <= j) {\n if (nums[i] == -nums[j]) return abs(nums[i]);\n else if (nums[i] < -nums[j]) i++;\n else j--;\n }\n return -1;\n }\n};",
"memory": "23400"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& arr) {\n sort(arr.begin(),arr.end());\n int lo=0,hi=arr.size()-1;\n while(lo<hi){\n if(arr[lo]>0)return -1;\n if(abs(arr[lo])==arr[hi])return arr[hi];\n else if(abs(arr[lo])<arr[hi])hi--;\n else lo++;\n }\n return -1;\n }\n};",
"memory": "23500"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n vector<bool> map(1001,false);\n int ans = -1;\n for(int i = 0;i<nums.size();i++)\n {\n if(nums[i]<0)\n {\n map[-nums[i]] = true;\n }\n else\n {\n if(map[nums[i]]) ans = max(ans,nums[i]);\n }\n }\n return ans;\n }\n};",
"memory": "23600"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) \n {\n int table_p[2001]={0};\n int max=0;\n for(int i=0;i<nums.size();i++)\n {\n table_p[nums[i]+1000]=1;\n if(table_p[-nums[i]+1000]==1)\n {\n if(nums[i]<0)\n {\n if(max<-nums[i])\n max=-nums[i];\n }\n else\n if(max<nums[i])\n max=nums[i];\n\n }\n }\n if(max==0)\n return -1;\n return max;\n }\n};",
"memory": "23700"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) \n {\n int table_p[2001]={0};\n int max=0;\n for(int i=0;i<nums.size();i++)\n {\n table_p[nums[i]+1000]=1;\n if(table_p[-nums[i]+1000]==1)\n {\n\n if(max<-nums[i])\n max=-nums[i];\n if(max<nums[i])\n max=nums[i];\n }\n }\n if(max==0)\n return -1;\n return max;\n }\n};",
"memory": "23700"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int pos[1001] = {0};\n int neg[1001] = {0};\n int k = -1;\n for (auto num: nums) {\n if (num < 0) {\n if (pos[-num]) k = max(k, -num);\n neg[-num]++;\n } else {\n if (neg[num]) k = max(k, num);\n pos[num]++;\n }\n }\n return k;\n }\n};",
"memory": "23800"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int arr[1001] = {0};\n int solution = -1;\n for (int i = 0; i < nums.size(); ++i) {\n int module_i = abs(nums[i]);\n if (arr[module_i] == nums[i])\n continue;\n arr[module_i] += nums[i];\n if (arr[module_i] == 0)\n solution = max(solution, module_i);\n }\n return solution;\n }\n};",
"memory": "23800"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int arr[1001] = {0};\n int solution = -1;\n for (int i = 0; i < nums.size(); ++i) {\n int module_i = abs(nums[i]);\n if (arr[module_i] == nums[i])\n continue;\n arr[module_i] += nums[i];\n if (arr[module_i] == 0)\n solution = max(solution, module_i);\n }\n return solution;\n }\n};",
"memory": "23900"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int arr[1001] ={0};\n int solution = -1;\n for (int i = 0; i < nums.size(); ++i) {\n int module_i = abs(nums[i]);\n if (arr[module_i] == nums[i])\n continue;\n arr[module_i] += nums[i];\n if (arr[module_i] == 0)\n solution = max(solution, module_i);\n }\n return solution;\n }\n};",
"memory": "24000"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<int>pos;\n vector<int>neg;\n int ans= 0;\n int b=0;\n int n = nums.size();\n for(int i =0;i<n;i++){\n if(nums[i]<0){\n neg.push_back(nums[i]);\n }\n else{\n pos.push_back(nums[i]);\n }\n }\n\n\n\n for(int i=0;i<neg.size();i++){\n for(int j=0;j<pos.size();j++){\n \n if(neg[i]== -pos[j]) {\n b= pos[j];\n }\n \n ans =max(ans,b);\n }\n\n }\n if(ans==0)\n return -1;\n else\n return ans;\n\n }\n};",
"memory": "24100"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) \n {\n int nums_buffer[1001]{};\n vector<int> considerations;\n\n for(int i = 0; i < nums.size(); i++)\n {\n int sign = nums[i] < 0 ? -1 : 1;\n int num = sign * nums[i];\n if(sign == -1 && nums_buffer[num] == 1) considerations.push_back(num);\n else if(sign == 1 && nums_buffer[num] == -1) considerations.push_back(num);\n nums_buffer[num] = sign;\n }\n\n int result = -1;\n for(int consideration : considerations) result = consideration > result ? consideration : result;\n return result;\n }\n};",
"memory": "24300"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) \n {\n int nums_buffer[1001]{};\n vector<int> considerations;\n\n for(int i = 0; i < nums.size(); i++)\n {\n int sign = nums[i] < 0 ? -1 : 1;\n int num = sign * nums[i];\n if(sign == -1 && nums_buffer[num] == 1) considerations.push_back(num);\n else if(sign == 1 && nums_buffer[num] == -1) considerations.push_back(num);\n nums_buffer[num] = sign;\n }\n\n int result = -1;\n for(int consideration : considerations) result = consideration > result ? consideration : result;\n return result;\n }\n};\nauto f = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n return 0;\n}();",
"memory": "24400"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) \n {\n int nums_buffer[1001]{};\n vector<int> considerations;\n\n for(int i = 0; i < nums.size(); i++)\n {\n int sign = nums[i] < 0 ? -1 : 1;\n int num = sign * nums[i];\n if(sign == -1 && nums_buffer[num] == 1) considerations.push_back(num);\n else if(sign == 1 && nums_buffer[num] == -1) considerations.push_back(num);\n nums_buffer[num] = sign;\n }\n\n int result = -1;\n for(int consideration : considerations) result = consideration > result ? consideration : result;\n return result;\n }\n};\nauto f = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n return 0;\n}();",
"memory": "24400"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n set<int> st;\n for(auto it: nums){\n if(it<0) st.insert(it);\n }\n\n int maxi=-1;\n for(auto i:nums){\n if(i>0){\n int m = i-(2*i);\n if(st.find(m) != st.end()){\n if(i>maxi) maxi = i;\n }\n }\n }\n return maxi;\n\n }\n};",
"memory": "24500"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int max = -1;\n std::set<int> negs;\n\n for (int i = 0; i < nums.size(); i++){\n if (nums[i] < 0){\n negs.insert(nums[i]);\n }\n }\n\n for (int i = 0; i < nums.size(); i++){\n if (nums[i] > 0){\n if (negs.find(-nums[i]) != negs.end()){\n max = std::max(max, nums[i]);\n }\n }\n }\n\n return max;\n }\n};",
"memory": "24600"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution\n{\npublic:\n int findMaxK(vector<int> &nums)\n {\n\n vector<int> save;\n int max = -1;\n\n for (int i = 0; i < nums.size() - 1; i++)\n {\n for (int j = i + 1; j < nums.size(); j++)\n {\n if ((abs(nums[i]) == abs(nums[j])) && (((nums[i] < 0) && (nums[j] > 0)) || ((nums[j] < 0) && (nums[i] > 0))))\n\n {\n save.push_back((abs)(nums[i]));\n }\n }\n }\n if (save.size() != 0)\n {\n max = save[0];\n for (int i = 1; i < save.size(); i++)\n {\n if (save[i] > max)\n {\n max = save[i];\n }\n }\n }\n return max;\n }\n};",
"memory": "24700"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<int>neg;\n vector<int>pos;\n sort(nums.begin(),nums.end());\n int n=nums.size();\n for(int i=0;i<n;i++)\n {\n if(nums[i]<0)\n {\n neg.push_back(nums[i]);\n }\n else\n {\n pos.push_back(nums[i]);\n }\n }\n for(int i=0;i<neg.size();i++)\n {\n for(int j=0;j<pos.size();j++)\n {\n if((neg[i]*-1)==pos[j])\n {\n return pos[j];\n }\n }\n }\n return -1;\n }\n};",
"memory": "24800"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<int>v;\n for(int i=0;i<nums.size();i++)\n {\n for(int j=i+1;j<nums.size();j++)\n {\n if(nums[i]==(-nums[j]))\n {\n v.push_back(abs(nums[i]));\n }\n }\n }\n if(v.size()==0)\n {\n return -1;\n }\n else\n {\n int l=INT_MIN;\n for(int i=0;i<v.size();i++)\n {\n if(v[i]>l)\n {\n l=v[i];\n }\n }\n return l; \n } \n }\n};",
"memory": "24900"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n priority_queue<int> positives; \n priority_queue<int, vector<int>, greater<int>> negatives;\n\n for (int num : nums) {\n if (num > 0) {\n positives.push(num);\n } else if (num < 0) {\n negatives.push(num);\n }\n }\n\n while (!positives.empty() && !negatives.empty()) {\n int pos = positives.top();\n int neg = negatives.top();\n\n if (pos == -neg) {\n return pos;\n }\n\n if (pos > -neg) {\n positives.pop(); \n } else {\n negatives.pop(); \n }\n }\n\n return -1;\n }\n};\n",
"memory": "25200"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int maxValue=-1;\n unordered_set<int> st;\n for(int i=0;i<nums.size();i++) {\n if(nums[i] < 0) {\n st.insert(nums[i]);\n }\n }\n for(int i=0;i<nums.size();i++) {\n if(nums[i]>0 && st.find(-nums[i]) != st.end()) {\n maxValue=max(maxValue,nums[i]);\n }\n }\n return maxValue;\n }\n};",
"memory": "25300"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n unordered_map<int , int> counter;\n for(int i = 0 ; i < nums.size() ; i++){\n if(nums[i] < 0) counter[nums[i]]++;\n }\n int res = INT_MIN;\n for(int i = 0; i < nums.size() ; i++){\n if(nums[i] > 0 && counter.find(-1 * nums[i]) != counter.end()){\n res = max(res , nums[i]);\n }\n }\n if(res == INT_MIN) return -1;\n return res;\n }\n};",
"memory": "25400"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int maxValue=-1;\n unordered_set<int> st;\n for(int i=0;i<nums.size();i++) {\n if(nums[i] < 0) {\n st.insert(nums[i]);\n }\n }\n for(int i=0;i<nums.size();i++) {\n if(nums[i]>0 && st.find(-nums[i]) != st.end()) {\n maxValue=max(maxValue,nums[i]);\n }\n }\n return maxValue;\n }\n};",
"memory": "25500"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n unordered_set<int> positiveSet;\n int maxK = -1;\n\n //first pass\n for (int num : nums){\n if (num > 0){\n positiveSet.insert(num);\n }\n }\n //second pass\n for (int num : nums){\n if (num < 0 && positiveSet.find(-num) != positiveSet.end()){\n maxK = max(maxK,-num);\n }\n }\n return maxK;\n }\n};",
"memory": "25600"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int maxValue=-1;\n unordered_set<int> st;\n for(int i=0;i<nums.size();i++) {\n if(nums[i] < 0) {\n st.insert(nums[i]);\n }\n }\n for(int i=0;i<nums.size();i++) {\n if(nums[i]>0 && st.find(-nums[i]) != st.end()) {\n maxValue=max(maxValue,nums[i]);\n }\n }\n return maxValue;\n }\n};",
"memory": "25700"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n\n // sort(nums.begin(),nums.end());\n // for(int i=nums.size()-1;i>=0;i--)\n // {\n // for(int j=i-1;j>=0;j--)\n // {\n // if(nums[i]+nums[j]==0)\n // {\n // return nums[i];\n // }\n // }\n // }\n // return -1;\n\n // In that question i have used two approach one is reverse the number and second one is do not reverse the number\n\n sort(nums.rbegin(),nums.rend());\n for(int i=0;i<nums.size();i++)\n {\n for(int j=i+1;j<nums.size();j++)\n {\n if(nums[i]+nums[j]==0)\n {\n return nums[i];\n }\n }\n }\n return -1;\n \n }\n};",
"memory": "25800"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n vector<int> myvector;\n for(int i=0;i<nums.size();i++)\n {\n for(int j=i+1;j<nums.size();j++)\n {\n if(nums[i]==-(nums[j]))\n {\n myvector.push_back(abs(nums[i]));\n }\n }\n }\n if(myvector.empty()) \n {\n return -1;\n }\n sort(myvector.begin(),myvector.end());\n return myvector.back();\n }\n};",
"memory": "25900"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int findMaxK(vector<int>& nums) {\n int ans = -1;\n unordered_set<int> seen;\n\n for (const int num : nums)\n if (seen.contains(-num))\n ans = max(ans, abs(num));\n else\n seen.insert(num);\n\n return ans;\n }\n};",
"memory": "26500"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "#include<bits/stdc++.h>\nusing namespace std;\nclass Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n\t\t// keep track of maximum\n int maxi = -1;\n\t\t// set to remeber the other value \n unordered_set<int> s;\n for(auto i : nums){\n if(s.find(-i) == s.end()){\n s.insert(i);\n }else{\n\t\t\t\t// if found update maxi \n maxi = max(maxi, abs(i));\n }\n }\n\t\t// if not present return -1 else return maxi\n return maxi ;\n }\n};\n// time complexiy O(n) \n// space complexity O(n)",
"memory": "26600"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "#include<bits/stdc++.h>\nusing namespace std;\nclass Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n\t\t// keep track of maximum\n int maxi = -1;\n\t\t// set to remeber the other value \n unordered_set<int> s;\n for(auto i : nums){\n if(s.find(i * -1) == s.end()){\n\t\t\t\t// if not found insert\n s.insert(i);\n }else{\n\t\t\t\t// if found update maxi \n maxi = max(maxi, abs(i));\n }\n }\n\t\t// if not present return -1 else return maxi\n return maxi ;\n }\n};\n// time complexiy O(n) \n// space complexity O(n)",
"memory": "26700"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n std::unordered_set<int> num_set(nums.begin(), nums.end());\n int largest_k=-1;\n for (int num : nums) {\n if (num_set.find(-num) !=num_set.end()) {\n largest_k = std::max(largest_k, std::abs(num));\n }\n }\n return largest_k !=-1 ? largest_k : -1;\n }\n};",
"memory": "26800"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n set<int> s(nums.begin(), nums.end());\n int ans = -1;\n sort(nums.begin(), nums.end());\n for (int i=0; i<nums.size(); i++){\n if (nums[i]<0 && s.find(-1*nums[i])!=s.end()){\n ans = nums[i]*-1;\n break;\n }\n }\n return ans;\n }\n};",
"memory": "26800"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n unordered_set<int> mpp(nums.begin() , nums.end());\n sort(nums.begin() , nums.end());\n for(int i = nums.size()-1 ; i>= 0 ; i--){\n if(mpp.find(-1 * nums[i]) != mpp.end()){\n return nums[i];\n }\n }\n return -1;\n \n }\n};",
"memory": "26900"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n /*sort(nums.begin(), nums.end());\n\n int l = 0, r = nums.size()-1;\n\n while (l < r)\n {\n if ((-1 * nums[l]) == nums[r])\n return nums[r];\n else if ((-1 * nums[l]) > nums[r])\n ++l;\n else\n --r;\n }\n\n return -1;\n */\n\n std::unordered_map<int, int> m;\n for (int i = 0; i < nums.size(); ++i)\n {\n m[nums[i]] = i;\n }\n\n int maxv = std::numeric_limits<int>::min();\n for (auto val : nums)\n {\n if (m.contains(-val))\n {\n maxv = std::max(maxv, std::abs(val));\n }\n }\n \n if (maxv != std::numeric_limits<int>::min())\n return maxv;\n else\n return -1;\n }\n};",
"memory": "26900"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n unordered_set<int> numSet;\n int maxK = -1;\n //traversing\n for (int num : nums){\n if (numSet.find(-num)!= numSet.end()){\n maxK = max(maxK,abs(num));\n }\n numSet.insert(num);\n }\n return maxK;\n }\n};",
"memory": "27000"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int ans = -1;\n\n unordered_set<int> seen;\n\n for (int num : nums) {\n const int abs_num = abs(num);\n\n if (abs_num > ans && seen.contains(-num)) {\n ans = abs_num;\n }\n\n seen.insert(num);\n }\n\n return ans;\n }\n};\n",
"memory": "27100"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(std::vector<int>& nums) {\n std::unordered_set<int> num_set(nums.begin(), nums.end());\n int largest_k = -1;\n\n for (int num : nums) {\n if (num_set.find(-num) != num_set.end()) {\n largest_k = std::max(largest_k, std::abs(num));\n }\n }\n\n return largest_k != -1 ? largest_k : -1;\n }\n};",
"memory": "27200"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n unordered_set<int> s(nums.begin(), nums.end());\n int ans = -1;\n for (int x : s) {\n if (s.count(-x)) {\n ans = max(ans, x);\n }\n }\n return ans;\n }\n};",
"memory": "27200"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n std::unordered_set<int> set(nums.cbegin(), nums.cend());\n int max = -1;\n for(const auto num : nums)\n {\n if(set.contains(num * -1))\n max = std::max(max, std::abs(num));\n }\n return max;\n }\n};",
"memory": "27300"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int maxValue=-1;\n unordered_set<int> st;\n unordered_set<int> st1;\n for(int i=0;i<nums.size();i++) {\n if(nums[i] < 0) {\n st.insert(nums[i]);\n }\n }\n for(int i=0;i<nums.size();i++) {\n if(nums[i] > 0) {\n st1.insert(nums[i]);\n }\n }\n for(auto& it : st1) {\n if(st.find(-it) != st.end()) {\n maxValue=max(maxValue,it);\n }\n }\n return maxValue;\n }\n};",
"memory": "27400"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& a) {\n sort(a.begin(),a.end());\n map<int,int>mp;\n for(auto i:a)\n {\n mp[i]++;\n }\n int k=-1;\n for(int i=0;i<a.size();i++)\n {\n int value=a[i];\n if(mp.find(-value)!=mp.end())\n {\n k=value;\n }\n }\n return k;\n }\n};",
"memory": "27400"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n map<int,int>ff;\n int i,j;\n sort(nums.begin(),nums.end());\n for(i=0;i<nums.size();i++)\n {\n ff[nums[i]]++;\n }\n for(i=nums.size()-1;i>=0;i--)\n {\n if(nums[i]<0)\n {\n return -1;\n }\n j=nums[i]*(-1);\n if(ff[j])\n {\n return nums[i];\n }\n }\n return -1;\n }\n};",
"memory": "27500"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n unordered_map<int, bool> map;\n for(int num: nums){\n map[num] = true;\n }\n int max = INT_MIN;\n for(int i = 0; i < nums.size(); i++){\n if(nums[i] > max && map[nums[i] - 2*nums[i]]){\n max = nums[i];\n }\n }\n return max == INT_MIN ? -1 : max;\n }\n};",
"memory": "27600"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n\n vector<int>temp;\n\n for(int i=0; i<nums.size();i++){\n for(int j=0;j<nums.size();j++){\n if((nums[i] == (nums[j]*(-1))) && (i!=j)){\n int tmp =max(nums[i], nums[j]);\n temp.push_back(tmp);\n }\n }\n }\n\n if(temp.size() ==0){\n return -1;\n }\n sort(temp.begin(),temp.end());\n return temp[temp.size()-1];\n \n }\n};",
"memory": "27700"
} |
2,524 | <p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,2,-3,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
<strong>Output:</strong> 7
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is no a single valid k, we return -1.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 1000</code></li>
<li><code>-1000 <= nums[i] <= 1000</code></li>
<li><code>nums[i] != 0</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findMaxK(vector<int>& nums) {\n int ans=-1;\n int n=nums.size();\n unordered_map<int,int>mp;\n for(int i=0;i<n;i++){\n mp[nums[i]]++;\n }\n for(int i=0;i<n;i++){\n if(nums[i]>0 && mp[-nums[i]])ans=max(ans,nums[i]);\n }\n return ans;\n }\n};",
"memory": "27800"
} |
2,527 | <p>You are given an integer array <code>nums</code> and two integers <code>minK</code> and <code>maxK</code>.</p>
<p>A <strong>fixed-bound subarray</strong> of <code>nums</code> is a subarray that satisfies the following conditions:</p>
<ul>
<li>The <strong>minimum</strong> value in the subarray is equal to <code>minK</code>.</li>
<li>The <strong>maximum</strong> value in the subarray is equal to <code>maxK</code>.</li>
</ul>
<p>Return <em>the <strong>number</strong> of fixed-bound subarrays</em>.</p>
<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2,7,5], minK = 1, maxK = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,1], minK = 1, maxK = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], minK, maxK <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int minK, int maxK) {\n long long i = 0, ans = 0;\n long long n = nums.size();\n while (i < n) {\n\n // Skip if that element is not in given range\n if (nums[i] < minK || nums[i] > maxK) {\n i++;\n continue;\n }\n // Step 1 : Find Range of numbers contains only elements from minK to maxK\n int j = i;\n while (j < n && nums[j] >= minK && nums[j] <= maxK) {\n j++;\n }\n\n\n //Step 2 : Now you have a range i to j - 1 containg valid elems\n // Maintain two counters for minElemK and maxElemK\n long long k = i;\n long long cntmn = 0, cntmx = 0;\n while (k < j) {\n\n // Note -> you have a range i to k to j\n // So lets i to k have both minK and maxK\n // So ans will be increase by j - k + 1 considering i is fixed\n // because subarrays are i..k , i..k+1,i...k+2 ... , i + j - 1 \n\n\n if (cntmn > 0 && cntmx > 0) {\n ans += (j - k + 1);\n // contracting Sliding window\n if (nums[i] == minK)\n cntmn--;\n if (nums[i] == maxK)\n cntmx--;\n i++;\n } else {\n // Expanding window\n if (nums[k] == minK)\n cntmn++;\n if (nums[k] == maxK)\n cntmx++;\n k++;\n }\n }\n \n \n // Step 3 : At last k reach to j \n // Just contract till condition satisfy \n // Increase ans by (j - k + 1) as k = j just ans++\n while (i < j && (cntmx > 0 && cntmn > 0)) {\n ans++;\n if (nums[i] == minK)\n cntmn--;\n if (nums[i] == maxK)\n cntmx--;\n i++;\n };\n\n // \n i = j;\n }\n return ans;\n }\n};",
"memory": "82600"
} |
2,527 | <p>You are given an integer array <code>nums</code> and two integers <code>minK</code> and <code>maxK</code>.</p>
<p>A <strong>fixed-bound subarray</strong> of <code>nums</code> is a subarray that satisfies the following conditions:</p>
<ul>
<li>The <strong>minimum</strong> value in the subarray is equal to <code>minK</code>.</li>
<li>The <strong>maximum</strong> value in the subarray is equal to <code>maxK</code>.</li>
</ul>
<p>Return <em>the <strong>number</strong> of fixed-bound subarrays</em>.</p>
<p>A <strong>subarray</strong> is a <strong>contiguous</strong> part of an array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,3,5,2,7,5], minK = 1, maxK = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,1,1,1], minK = 1, maxK = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i], minK, maxK <= 10<sup>6</sup></code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n long long countSubarrays(vector<int>& nums, int minK, int maxK) {\n long long res = 0;\n int bad_idx = -1, left_idx = -1, right_idx = -1;\n\n for (int i = 0; i < nums.size(); ++i) {\n if (!(minK <= nums[i] && nums[i] <= maxK)) {\n bad_idx = i;\n }\n\n if (nums[i] == minK) {\n left_idx = i;\n }\n\n if (nums[i] == maxK) {\n right_idx = i;\n }\n\n res += max(0, min(left_idx, right_idx) - bad_idx);\n }\n\n return res;\n }\n};",
"memory": "82700"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.