id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool mylowestCommonAncestor(TreeNode*& root, TreeNode*& p, TreeNode*& q,\n TreeNode*& pLCA, bool& leftFound) {\n if (!root || pLCA)\n return false;\n else if (leftFound && (p == root || q == root))\n return true;\n bool bLeftFound =\n mylowestCommonAncestor(root->left, p, q, pLCA, leftFound);\n if (bLeftFound && (p == root || q == root)) {\n pLCA = root;\n return false;\n }\n else if (bLeftFound)\n leftFound = bLeftFound;\n bool bRightFound =\n mylowestCommonAncestor(root->right, p, q, pLCA, leftFound);\n if ((bLeftFound && bRightFound) ||\n ((bLeftFound || bRightFound) && (p == root || q == root))) {\n pLCA = root;\n return false;\n }\n return bLeftFound || bRightFound || p == root || q == root;\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n auto init = []() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n }();\n if (!root)\n return NULL;\n TreeNode* LCA = NULL;\n bool b = false;\n mylowestCommonAncestor(root, p, q, LCA, b);\n return LCA;\n }\n};\n",
"memory": "16800"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool path(TreeNode * root, TreeNode * p, vector<int>&res){\n if(!root) return false;\n if(root->val==p->val){ res.push_back(root->val); return true;}\n res.push_back(root->val);\n if(path(root->right,p,res) || path(root->left,p,res))\n return true;\n res.pop_back();\n return false;\n\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<int>res;\n vector<int>res2;\n bool r1=path(root,q,res);\n bool r2=path(root,p,res2);\n int ele=-1e8;\n int mi=min(res.size(),res2.size());\n\n for(auto i:res) cout<<i<<\" \";\n cout<<endl;\n for(auto i:res2) cout<<i<<\" \";\n for(int i=0;i<mi;i++){\n if(res[i]!=res2[i]){\n TreeNode * temp=new TreeNode(ele);\n return temp;\n }\n else{\n ele=res[i];\n }\n }\n if(ele!=-1e8){\n TreeNode * temp=new TreeNode(ele);\n return temp;\n }\n \n return root;\n\n \n }\n};",
"memory": "16900"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* ans=NULL;\n bool find(TreeNode* root, TreeNode* p, TreeNode* q){\n if(!root)return false;\n if(root==p || root==q)return true;\n return find(root->left,p,q)||find(root->right,p,q);\n }\n void fun(TreeNode* root, TreeNode* p, TreeNode* q){\n if(!root)return;\n if(root==p && (find(root->left,p,q) || find(root->right,p,q) )){ ans=root; return;}\n if(root==q && (find(root->left,p,q) || find(root->right,p,q) )){ ans=root; return;}\n if(find(root->right,p,q)&&find(root->left,p,q)){\n ans=root;\n }\n fun(root->left,p,q);\n fun(root->right,p,q);\n return;\n\n\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n \n \n if(root==p && (find(root->left,p,q) || find(root->right,p,q) ))return root;\n if(root==q && (find(root->left,p,q) || find(root->right,p,q) ))return root;\n fun(root,p,q);\n return ans;\n }\n};",
"memory": "17000"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* ans=NULL;\n bool find(TreeNode* root, TreeNode* p, TreeNode* q){\n if(!root)return false;\n if(root==p || root==q)return true;\n return find(root->left,p,q)||find(root->right,p,q);\n }\n void fun(TreeNode* root, TreeNode* p, TreeNode* q){\n if(!root)return;\n if(root==p && (find(root->left,p,q) || find(root->right,p,q) )){ ans=root; return;}\n if(root==q && (find(root->left,p,q) || find(root->right,p,q) )){ ans=root; return;}\n if(find(root->right,p,q)&&find(root->left,p,q)){\n ans=root;\n }\n fun(root->left,p,q);\n fun(root->right,p,q);\n return;\n\n\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n \n \n // if(root==p && (find(root->left,p,q) || find(root->right,p,q) ))return root;\n // if(root==q && (find(root->left,p,q) || find(root->right,p,q) ))return root;\n fun(root,p,q);\n return ans;\n }\n};",
"memory": "17000"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void solve(TreeNode* root, TreeNode* target, vector<int>& ans, vector<int>& res) {\n if (!root) return;\n \n res.push_back(root->val); // Push current node's value to the path\n\n if (root == target) {\n ans = res; // If target node is found, save the current path in ans\n }\n \n solve(root->left, target, ans, res); // Recur on left subtree\n solve(root->right, target, ans, res); // Recur on right subtree\n \n res.pop_back(); // Backtrack by removing the current node's value from the path\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<int> ans1, ans2, res;\n\n solve(root, p, ans1, res); // Find path from root to node p\n res.clear(); // Clear res vector to reuse it for the second path\n solve(root, q, ans2, res); // Find path from root to node q\n\n int i;\n for (i = 0; i < min(ans1.size(), ans2.size()); i++) {\n if (ans1[i] != ans2[i]) break;\n }\n \n return new TreeNode(ans1[i - 1]); // Return the last common node\n }\n};",
"memory": "17100"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void inorder(TreeNode* root, vector<int>& v) {\n if (root == NULL) return;\n inorder(root->left, v);\n v.push_back(root->val);\n inorder(root->right, v);\n }\n\n void preorder(TreeNode* root, vector<int>& v) {\n if (root == NULL) return;\n v.push_back(root->val);\n preorder(root->left, v);\n preorder(root->right, v);\n }\n\n TreeNode* findNode(TreeNode* root, int value) {\n if (root == NULL) return NULL;\n if (root->val == value) return root;\n TreeNode* left = findNode(root->left, value);\n if (left) return left;\n return findNode(root->right, value);\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<int> pre, in;\n inorder(root, in);\n preorder(root, pre);\n\n int pi = p->val, qi = q->val;\n auto pre_pi = find(pre.begin(), pre.end(), pi);\n auto pre_qi = find(pre.begin(), pre.end(), qi);\n\n if (pre_pi == pre.end() || pre_qi == pre.end()) return NULL;\n\n int c = distance(pre.begin(), pre_pi);\n int d = distance(pre.begin(), pre_qi);\n int j = min(c, d);\n\n auto in_pi = find(in.begin(), in.end(), pi);\n auto in_qi = find(in.begin(), in.end(), qi);\n\n if (in_pi == in.end() || in_qi == in.end()) return NULL;\n\n int a = distance(in.begin(), in_pi);\n int b = distance(in.begin(), in_qi);\n\n if (a > b) {\n swap(a, b);\n }\n\n for (int i = 0; i <= j; i++) {\n for (int k = a; k <= b; k++) {\n if (in[k] == pre[i]) {\n return findNode(root, pre[i]);\n }\n }\n }\n\n return NULL;\n }\n};\n",
"memory": "17200"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) \n {\n // conditions:\n // lowest node with the following true:\n // - both ancestors not found before visiting node\n // - both ancestors found after visiting node\n // - and is set only once.\n\n bool foundp = false, foundq = false;\n TreeNode* lca = nullptr;\n\n function<void(TreeNode*)> dfs = [&] (TreeNode* root)\n {\n if (root == nullptr)\n return;\n\n bool candidate = !foundp && !foundq;\n\n if (p == root)\n foundp = true;\n\n if (q == root)\n foundq = true;\n\n dfs (root->left);\n dfs (root->right);\n\n if (candidate && foundp && foundq && lca == nullptr)\n lca = root;\n };\n\n dfs (root);\n return lca; \n }\n};",
"memory": "17300"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n\n //ITERATIVE vs. RECUSRIVE\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n // return backtrack(root, p, q);\n \n\n stack<pair<TreeNode*, int>> s;\n s.push({root, 2});\n\n int state = 0;\n TreeNode* res = nullptr;\n while(!s.empty()) {\n auto pp = s.top(); s.pop();\n TreeNode* node = pp.first;\n\n if(pp.second == 2) {\n s.push({node,1});\n if(node->left != nullptr) {\n s.push({node->left, 2});\n }\n }else if(pp.second == 1) {\n s.push({node, 0});\n if(node->right != nullptr) {\n s.push({node->right, 2});\n }\n if(state == 1 && res == nullptr) {\n res = node;\n }\n } else {\n \n if(node == p || node == q) {\n state++;\n }\n\n if(state == 2) {\n if(res == nullptr) res = node;\n break;\n }else if(state == 1 && node == res) {\n res = nullptr;\n }\n }\n }\n\n return res;\n }\n\n TreeNode* backtrack(TreeNode* root, TreeNode* p, TreeNode* q){\n int state = 0;\n\n if(root == nullptr) return nullptr;\n\n TreeNode* left = backtrack(root->left, p, q);\n TreeNode* right = backtrack(root->right, p, q);\n if(left != nullptr) state++;\n if(right != nullptr) state++;\n\n if(state == 2 || root == p || root == q) return root;\n if(state == 1) return left != nullptr ? left : right;\n return nullptr;\n }\n};",
"memory": "17400"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n TreeNode* ans;\n\n function<int(TreeNode*)> dfs = [&](TreeNode* node) -> int {\n if (!node) return 0;\n\n int cur = node == q ? 1 : node == p ? 2 : 0;\n\n int ret1 = dfs(node->left);\n int ret2 = dfs(node->right);\n \n if (ret1 + ret2 == 3 || cur + ret1 == 3 || cur + ret2 == 3) {\n ans = node;\n }\n\n return max({cur, ret1, ret2});\n };\n\n dfs(root);\n\n return ans;\n }\n};",
"memory": "17500"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n TreeNode* ans;\n\n function<int(TreeNode*)> dfs = [&](TreeNode* node) -> int {\n if (!node) return 0;\n\n int cur = node == q ? 1 : node == p ? 2 : 0;\n\n int ret1 = dfs(node->left);\n int ret2 = dfs(node->right);\n \n if (ret1 + ret2 == 3 || cur + ret1 == 3 || cur + ret2 == 3) {\n ans = node;\n }\n\n return max({cur, ret1, ret2});\n };\n\n dfs(root);\n\n return ans;\n }\n};",
"memory": "17600"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n TreeNode* ans;\n\n function<int(TreeNode*)> dfs = [&](TreeNode* node) -> int {\n if (!node) return 0;\n\n int cur = node == q ? 1 : node == p ? 2 : 0;\n\n int ret1 = dfs(node->left);\n int ret2 = dfs(node->right);\n \n if (ret1 + ret2 == 3 || cur + ret1 == 3 || cur + ret2 == 3) {\n ans = node;\n }\n\n return max({cur, ret1, ret2});\n };\n\n dfs(root);\n\n return ans;\n }\n};",
"memory": "17700"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void dfs(vector<TreeNode*> &wektor, TreeNode* &p, bool &bol){\n if(bol && wektor.back()->left == p){\n wektor.push_back(wektor.back()->left);\n bol = false;\n }\n if(bol && wektor.back()->left != nullptr){\n wektor.push_back(wektor.back()->left);\n dfs(wektor, p, bol);\n }\n if(bol && wektor.back()->right == p){\n wektor.push_back(wektor.back()->right);\n bol = false;\n }\n if(bol && wektor.back()->right != nullptr){\n wektor.push_back(wektor.back()->right);\n dfs(wektor, p, bol);\n }\n if(bol){\n wektor.pop_back();\n }\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<TreeNode*> wektor_p;\n vector<TreeNode*> wektor_q;\n bool bol_p = true;\n bool bol_q = true;\n wektor_p.push_back(root);\n wektor_q.push_back(root);\n dfs(wektor_p, p, bol_p);\n dfs(wektor_q, q, bol_q);\n\n for(int i=wektor_p.size()-1; i>=0; i--){\n for(int j=wektor_q.size()-1; j>=0; j--){\n if(wektor_p[i] == wektor_q[j]){\n return wektor_p[i];\n }\n }\n }\n return root;\n }\n};",
"memory": "17800"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n\n void dfs(vector<TreeNode*> &wektor, TreeNode* &p, bool &bol){\n if(bol && wektor.back()->left == p){\n wektor.push_back(wektor.back()->left);\n bol = false;\n }\n if(bol && wektor.back()->left != nullptr){\n wektor.push_back(wektor.back()->left);\n dfs(wektor, p, bol);\n }\n if(bol && wektor.back()->right == p){\n wektor.push_back(wektor.back()->right);\n bol = false;\n }\n if(bol && wektor.back()->right != nullptr){\n wektor.push_back(wektor.back()->right);\n dfs(wektor, p, bol);\n }\n if(bol){\n wektor.pop_back();\n }\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<TreeNode*> wektor_p;\n vector<TreeNode*> wektor_q;\n bool bol_p = true;\n bool bol_q = true;\n wektor_p.push_back(root);\n wektor_q.push_back(root);\n dfs(wektor_p, p, bol_p);\n dfs(wektor_q, q, bol_q);\n\n for(int i=wektor_p.size()-1; i>=0; i--){\n for(int j=wektor_q.size()-1; j>=0; j--){\n if(wektor_p[i] == wektor_q[j]){\n return wektor_p[i];\n }\n }\n }\n return root;\n }\n};",
"memory": "17800"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\n #include<unordered_map>\nclass Solution {\n typedef TreeNode* ptr ;\n typedef TreeNode node ;\npublic:\n ptr lowestCommonAncestor(ptr root, ptr p, ptr q) \n {\n if (p == root) { return p; }\n if (q == root) { return q; }\n if (Ancestorof(p, q)) { return p ;}\n if (Ancestorof(q, p)) { return q ;}\n\n // Hash map nodes to parents.\n std :: unordered_map<ptr, ptr> parent ;\n FillMap(root, parent) ;\n\n return lowestCommonAncestor(root, parent[p], parent[q]) ;\n }\n\nprivate:\n bool Ancestorof(ptr p, ptr q)\n {\n if (p == nullptr)\n {\n return false;\n }\n if (p==q)\n {\n return true ;\n }\n if (Ancestorof(p->left, q))\n {\n return true;\n }\n return Ancestorof(p->right, q) ;\n }\n\n void FillMap(ptr root, std :: unordered_map<ptr, ptr>& parent)\n {\n if (root==nullptr)\n {\n return ;\n }\n if (root->left)\n {\n parent[root->left] = root ;\n FillMap(root->left, parent) ;\n }\n if (root->right)\n {\n parent[root->right] = root ;\n FillMap(root->right, parent) ;\n }\n return ;\n }\n};",
"memory": "17900"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\n #include<unordered_map>\nclass Solution {\n typedef TreeNode* ptr ;\n typedef TreeNode node ;\npublic:\n ptr lowestCommonAncestor(ptr root, ptr p, ptr q) \n {\n if (p == root) { return p; }\n if (q == root) { return q; }\n if (Ancestorof(p, q)) { return p ;}\n if (Ancestorof(q, p)) { return q ;}\n\n // Hash map nodes to parents.\n std :: unordered_map<ptr, ptr> parent ;\n FillMap(root, parent) ;\n\n return lowestCommonAncestor(root, parent[p], parent[q]) ;\n }\n\nprivate:\n bool Ancestorof(ptr p, ptr q)\n {\n if (p == nullptr)\n {\n return false;\n }\n if (p==q)\n {\n return true ;\n }\n if (Ancestorof(p->left, q))\n {\n return true;\n }\n return Ancestorof(p->right, q) ;\n }\n\n void FillMap(ptr root, std :: unordered_map<ptr, ptr>& parent)\n {\n if (root==nullptr)\n {\n return ;\n }\n if (root->left)\n {\n parent[root->left] = root ;\n FillMap(root->left, parent) ;\n }\n if (root->right)\n {\n parent[root->right] = root ;\n FillMap(root->right, parent) ;\n }\n return ;\n }\n};",
"memory": "17900"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n \n bool pathFromRootToNode(TreeNode* root, TreeNode* child, std::vector<TreeNode*>& path) {\n if (!root) {\n return false;\n }\n\n if (root == child) {\n return true;\n }\n\n if (pathFromRootToNode(root->left, child, path)) {\n // cout << \"adding left: \" << root->left->val << endl;\n path.push_back(root->left);\n return true;\n }\n if (pathFromRootToNode(root->right, child, path)) {\n // cout << \"adding right: \" << root->right->val << endl;\n path.push_back(root->right);\n return true;\n }\n\n return false;\n }\n\n std::vector<TreeNode*> pathFromRootToNode(TreeNode* root, TreeNode* child) {\n std::vector<TreeNode*> path;\n path.reserve(1000);\n pathFromRootToNode(root, child, path);\n // cout << \"adding root: \" << root->val << endl;\n path.push_back(root);\n return path;\n }\n \n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n // cout << \"pathToP:\" << endl;\n std::vector<TreeNode*> pathToP = pathFromRootToNode(root, p);\n std::reverse(pathToP.begin(), pathToP.end());\n\n // cout << \"pathToP:\" << endl;\n std::vector<TreeNode*> pathToQ = pathFromRootToNode(root, q);\n std::reverse(pathToQ.begin(), pathToQ.end());\n\n int n = min(pathToP.size(), pathToQ.size());\n for (int i = 1; i < n; ++i) {\n // cout << \"comparing: \" << pathToP[i]->val << \" and \" << pathToQ[i]->val << endl;\n if (pathToP[i] != pathToQ[i]) {\n // cout << \"returing: \" << pathToP[i - 1]->val << endl;\n return pathToP[i - 1];\n }\n }\n return pathToP[n - 1];\n }\n};",
"memory": "18000"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n\n void lowest(TreeNode* root, TreeNode* p, TreeNode* q, int &count, TreeNode* &ans) {\n if (root==nullptr){\n return;\n }\n int temp=0;\n lowest(root->left, p,q,temp,ans);\n lowest(root->right, p,q,temp,ans);\n if (ans!=nullptr) return;\n if (root==p || root==q) temp++;\n if (temp==2){\n ans=root;\n return;\n }\n count+=temp;\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q){\n int count=0;\n TreeNode* res=nullptr;\n lowest(root,p,q,count,res);\n return res;\n }\n};",
"memory": "18100"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n bool p_seen=false, q_seen=false;\n\n return lca(root, p, q, p_seen, q_seen);\n }\n\n TreeNode* lca(TreeNode* root, TreeNode* p, TreeNode* q, bool& p_seen, bool& q_seen) {\n if (!root) {\n return nullptr;\n }\n\n bool p_seen_left = p_seen;\n bool q_seen_left = q_seen;\n TreeNode* left_ans = lca(root->left, p, q, p_seen_left, q_seen_left);\n if (left_ans) {\n return left_ans;\n }\n\n bool p_seen_right = p_seen;\n bool q_seen_right = q_seen;\n TreeNode* right_ans = lca(root->right, p, q, p_seen_right, q_seen_right);\n if (right_ans) {\n return right_ans;\n }\n\n if (p_seen_right || p_seen_left || root == p) {\n p_seen = true;\n }\n\n if (q_seen_right || q_seen_left || root == q) {\n q_seen = true;\n }\n\n if (p_seen && q_seen) {\n return root;\n }\n // if (root == p) {\n // p_seen = true;\n // if (q_seen) {\n // return root;\n // }\n // } else if (root == q) {\n // q_seen = true;\n // if (p_seen) {\n // return root;\n // }\n // }\n\n return nullptr;\n }\n};",
"memory": "18200"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n bool p_seen=false, q_seen=false;\n\n return lca(root, p, q, p_seen, q_seen);\n }\n\n TreeNode* lca(TreeNode* root, TreeNode* p, TreeNode* q, bool& p_seen, bool& q_seen) {\n if (!root) {\n return nullptr;\n }\n\n bool p_seen_left = p_seen;\n bool q_seen_left = q_seen;\n TreeNode* left_ans = lca(root->left, p, q, p_seen_left, q_seen_left);\n if (left_ans) {\n return left_ans;\n }\n\n bool p_seen_right = p_seen;\n bool q_seen_right = q_seen;\n TreeNode* right_ans = lca(root->right, p, q, p_seen_right, q_seen_right);\n if (right_ans) {\n return right_ans;\n }\n\n if (p_seen_right || p_seen_left || root == p) {\n p_seen = true;\n }\n\n if (q_seen_right || q_seen_left || root == q) {\n q_seen = true;\n }\n\n if (p_seen && q_seen) {\n return root;\n }\n // if (root == p) {\n // p_seen = true;\n // if (q_seen) {\n // return root;\n // }\n // } else if (root == q) {\n // q_seen = true;\n // if (p_seen) {\n // return root;\n // }\n // }\n\n return nullptr;\n }\n};",
"memory": "18200"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool find(TreeNode* root, TreeNode* p, vector<TreeNode*>& ans) {\n if (root == NULL) {\n return false;\n }\n if (root == p) {\n ans.push_back(p);\n return true;\n }\n if (find(root->left, p, ans)) {\n ans.push_back(root);\n return true;\n } else if (find(root->right, p, ans)) {\n ans.push_back(root);\n return true;\n }\n return false; \n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<TreeNode*> pPath;\n vector<TreeNode*> qPath;\n find(root, p, pPath);\n find(root, q, qPath);\n int i = pPath.size()-1;\n int j = qPath.size()-1;\n while((i >= 0) && (j >= 0) && (pPath[i] == qPath[j])) {\n i--;\n j--;\n }\n return pPath[i+1];\n }\n};",
"memory": "18700"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool findAncestors(stack<TreeNode*>& ancestors, int target, TreeNode* curr)\n {\n if (curr->val == target)\n {\n ancestors.push(curr);\n return true;\n }\n ancestors.push(curr);\n if (curr->left)\n {\n if (findAncestors(ancestors, target, curr->left))\n {\n return true;\n }\n }\n if (curr->right)\n {\n if (findAncestors(ancestors, target, curr->right))\n {\n return true;\n }\n }\n ancestors.pop();\n return false;\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n stack<TreeNode*> ancestors1, ancestors2;\n findAncestors(ancestors1, p->val, root);\n findAncestors(ancestors2, q->val, root);\n TreeNode* ans = root;\n int n1 = ancestors1.size(), n2 = ancestors2.size();\n if (n1 > n2)\n {\n for (int i = n1 - n2; i > 0; i--)\n {\n ancestors1.pop();\n }\n }\n else\n {\n for (int i = n2 - n1; i > 0; i--)\n {\n ancestors2.pop();\n }\n }\n while (ancestors1.top() != ancestors2.top())\n {\n ancestors1.pop();\n ancestors2.pop();\n }\n return ancestors1.top();\n }\n};",
"memory": "18800"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool check(TreeNode* root, TreeNode* p, TreeNode* q,bool& a ,bool& b){\n if(root==NULL) return false;\n if(root==p) a=true;\n if(root==q) b=true;\n check(root->left,p,q,a,b);\n check(root->right,p,q,a,b);\n\n return a&&b;\n \n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n if(root==NULL) return nullptr;\n // if(root->left ==p && root->right==q) return root;\n // pre order \n bool a = false, b = false;\n if (check(root, p, q, a, b)) {\n if (check(root->left, p, q, a = false, b = false)) {\n return lowestCommonAncestor(root->left, p, q);\n }\n if (check(root->right, p, q, a = false, b = false)) {\n return lowestCommonAncestor(root->right, p, q);\n }\n return root;\n }\n\n return nullptr;\n // return nullptr;\n }\n};",
"memory": "18800"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n TreeNode* res=NULL;\n int l=0, r=0;\n findlca(root, p, q, l, r, res);\n return res;\n }\n\n pair<bool,bool> findlca(TreeNode* root, TreeNode* p, TreeNode* q, int &l, int &r, TreeNode* &res){\n if(res)\n return make_pair(1,1);\n pair<bool, bool> lt = make_pair(0,0);\n pair<bool, bool> rt = make_pair(0,0);\n if(root->left)\n lt = findlca(root->left, p, q, l, r, res);\n if(res)\n return make_pair(1,1);\n if(root->right)\n rt = findlca(root->right, p, q, l, r, res);\n if(res)\n return make_pair(1,1);\n if(root==p&&(lt.second||rt.second)){\n res = root;\n return make_pair(1,1);\n }\n if(root==q&&(lt.first||rt.first)){\n res = root;\n return make_pair(1,1);\n }\n if(lt.first&&rt.second || lt.second&&rt.first){\n res = root;\n return make_pair(1,1);\n }\n // cout << root->val<<\" \";\n return make_pair(lt.first||rt.first||(root==p), rt.second||lt.second||(root==q));\n }\n};",
"memory": "18900"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\nvoid fillStacks(TreeNode* root, TreeNode* p, bool& found, stack<TreeNode*>& s) {\n if(!root || found) return;\n if(root == p){\n found = true;\n s.push(root);\n return ;\n }\n fillStacks(root->left,p,found,s);\n if(found) {\n s.push(root);\n return ;\n }\n fillStacks(root->right,p,found,s);\n if(found) {\n s.push(root);\n }\n}\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n stack<TreeNode*>sp;\n stack<TreeNode*>sq;\n bool found = false;\n fillStacks(root,p,found,sp);\n found = false;\n fillStacks(root,q,found,sq);\n TreeNode *lca = nullptr;\n while(!sp.empty() && !sq.empty()) {\n if(sp.top() == sq.top()) {\n lca = sp.top();\n sp.pop();\n sq.pop();\n } else break;\n }\n return lca;\n }\n};",
"memory": "19000"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool findAncestors(stack<TreeNode*>& ancestors, int target, TreeNode* curr)\n {\n if (curr->val == target)\n {\n ancestors.push(curr);\n return true;\n }\n ancestors.push(curr);\n if (curr->left)\n {\n if (findAncestors(ancestors, target, curr->left))\n {\n return true;\n }\n }\n if (curr->right)\n {\n if (findAncestors(ancestors, target, curr->right))\n {\n return true;\n }\n }\n ancestors.pop();\n return false;\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n stack<TreeNode*> ancestors1, ancestors2;\n findAncestors(ancestors1, p->val, root);\n findAncestors(ancestors2, q->val, root);\n TreeNode* ans = root;\n int n1 = ancestors1.size(), n2 = ancestors2.size();\n if (n1 > n2)\n {\n for (int i = n1 - n2; i > 0; i--)\n {\n ancestors1.pop();\n }\n }\n else\n {\n for (int i = n2 - n1; i > 0; i--)\n {\n ancestors2.pop();\n }\n }\n while (ancestors1.top() != ancestors2.top())\n {\n ancestors1.pop();\n ancestors2.pop();\n }\n return ancestors1.top();\n }\n};",
"memory": "19000"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool findAncestors(stack<TreeNode*>& ancestors, int target, TreeNode* curr)\n {\n if (curr->val == target)\n {\n ancestors.push(curr);\n return true;\n }\n ancestors.push(curr);\n if (curr->left)\n {\n if (findAncestors(ancestors, target, curr->left))\n {\n return true;\n }\n }\n if (curr->right)\n {\n if (findAncestors(ancestors, target, curr->right))\n {\n return true;\n }\n }\n ancestors.pop();\n return false;\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n stack<TreeNode*> ancestors1, ancestors2;\n findAncestors(ancestors1, p->val, root);\n findAncestors(ancestors2, q->val, root);\n TreeNode* ans = root;\n int n1 = ancestors1.size(), n2 = ancestors2.size();\n if (n1 > n2)\n {\n for (int i = n1 - n2; i > 0; i--)\n {\n ancestors1.pop();\n }\n }\n else\n {\n for (int i = n2 - n1; i > 0; i--)\n {\n ancestors2.pop();\n }\n }\n while (ancestors1.top() != ancestors2.top())\n {\n ancestors1.pop();\n ancestors2.pop();\n }\n return ancestors1.top();\n }\n};",
"memory": "19100"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool helper(TreeNode* root,stack<TreeNode*>& st,int z){\n if(root==nullptr) return false;\n st.push(root);\n if(root->val==z) return true;\n if(helper(root->left,st,z) || helper(root->right,st,z)) return true;\n st.pop();\n return false;\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n stack<TreeNode*> a;\n stack<TreeNode*> b;\n helper(root,a,p->val);\n helper(root,b,q->val);\n while(a.size()>b.size()) a.pop();\n while(b.size()>a.size()) b.pop();\n while((!a.empty()) && (!b.empty()) && a.top()!=b.top()){\n a.pop();\n b.pop();\n }\n return a.top();\n }\n};",
"memory": "19200"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n\n void helper(map<TreeNode*, TreeNode*> &parent, TreeNode* root)\n {\n if (root -> left)\n {\n parent[root -> left] = root;\n helper(parent, root -> left);\n }\n\n if (root -> right)\n {\n parent[root -> right] = root;\n helper(parent, root -> right);\n }\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n map<TreeNode*, TreeNode*> parent;\n helper(parent, root);\n int d1 = 0, d2 = 0;\n TreeNode* t = p;\n while (t!= root)\n {\n t = parent[t];\n d1++;\n }\n t = q;\n while (t!= root)\n {\n t = parent[t];\n d2++;\n }\n\n while (d1 < d2) {q = parent[q]; d2--;}\n while (d2 < d1) {p = parent[p]; d1--;}\n\n while(p != q)\n {\n p = parent[p];\n q = parent[q];\n }\n return p;\n }\n};",
"memory": "19300"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool findPath(TreeNode* root, TreeNode* target, vector<TreeNode*> &path){\n if(root == NULL) return false;\n\n path.push_back(root);\n\n if(root->val == target->val) return true;\n\n if(findPath(root->left,target,path) || findPath(root->right,target,path)) return true;\n\n path.pop_back();\n return false;\n\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<TreeNode*> for_p;\n vector<TreeNode*> for_q;\n if(!findPath(root,p,for_p)) return NULL;\n if(!findPath(root,q,for_q)) return NULL;\n \n int i;\n TreeNode* ans = NULL;\n for(int i=0; i<for_p.size() && i<for_q.size(); i++){\n if(for_p[i] == for_q[i]) ans = for_p[i];\n if(for_p[i] != for_q[i]) break;\n }\n return ans;\n }\n};",
"memory": "19400"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool findPath(TreeNode* root, TreeNode* target, vector<TreeNode*> &path){\n if(root == NULL) return false;\n\n path.push_back(root);\n\n if(root->val == target->val) return true;\n\n if(findPath(root->left,target,path) || findPath(root->right,target,path)) return true;\n\n path.pop_back();\n return false;\n\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<TreeNode*> for_p;\n vector<TreeNode*> for_q;\n if(!findPath(root,p,for_p)) return NULL;\n if(!findPath(root,q,for_q)) return NULL;\n \n int i;\n TreeNode* ans = NULL;\n for(int i=0; i<for_p.size() && i<for_q.size(); i++){\n if(for_p[i] == for_q[i]) ans = for_p[i];\n if(for_p[i] != for_q[i]) break;\n }\n return ans;\n }\n};\n\n// T.C = O(n)\n// S.C = O(n)",
"memory": "19500"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<TreeNode*> pre, in;\n preOrder(root, pre);\n inOrder(root, in);\n \n auto p0 = in.begin(), p1 = in.begin();\n while ((*p0)->val != p->val && (*p0)->val != q->val) p0 += 1;\n p1 = p0+1;\n while ((*p1)->val != p->val && (*p1)->val != q->val) p1 += 1;\n auto p2 = pre.begin();\n while ((*p2)->val != p->val && (*p2)->val != q->val) \n {\n cout << (*p2)->val << endl;\n if (find(p0, p1, *p2) != p1) return *p2;\n p2 += 1;\n }\n return *p2;\n }\n void preOrder(TreeNode* root, vector<TreeNode*>& pre)\n {\n if (root==nullptr) return;\n pre.push_back(root);\n preOrder(root->left, pre);\n preOrder(root->right, pre);\n }\n\n void inOrder(TreeNode* root, vector<TreeNode*>& in)\n {\n if (root==nullptr) return;\n inOrder(root->left, in);\n in.push_back(root);\n inOrder(root->right, in);\n }\n};",
"memory": "19500"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool pathtonode(vector<TreeNode*> &arr,TreeNode*root,TreeNode* p){\n if(root==NULL)return false;\n if(root==p){\n cout<<root->val<<endl;\n arr.push_back(root);\n return true;\n }\n if(pathtonode(arr,root->left,p)==true){\n cout<<root->val<<endl;\n arr.push_back(root);\n // arr.push_back(root->left);\n return true;\n }\n else if(pathtonode(arr,root->right,p)==true){\n cout<<root->val<<endl;\n arr.push_back(root);\n // arr.push_back(root->right);\n return true;\n }\n return false;\n }\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<TreeNode*> arr1;\n vector<TreeNode*>arr2;\n pathtonode(arr1,root,p);\n cout<<\"___\"<<endl;\n pathtonode(arr2,root,q);\n // arr1.push_back(p);\n // arr2.push_back(q);\n int i=arr1.size()-1;\n int j=arr2.size()-1;\n TreeNode*temp=root;\n while(i>=0 && j>=0){\n if(arr1[i]!=arr2[j])break;\n temp=arr1[i];\n i--;\n j--;\n }\n return temp;\n \n }\n};",
"memory": "19600"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *p, TreeNode *q)\n {\n vector<TreeNode *> pathp = findPath(root, p);\n vector<TreeNode *> pathq = findPath(root, q);\n\n TreeNode *lca = root;\n\n for (int i = 1; i < std::min(pathp.size(), pathq.size()); i++) {\n if (pathp[i]->val == pathq[i]->val) {\n lca = pathp[i];\n }\n else {\n return lca;\n }\n }\n return lca;\n }\n\nprivate:\n vector<TreeNode *> findPath(TreeNode *root, TreeNode *target)\n {\n vector<TreeNode *> path;\n _dfs(root, target, path);\n return path;\n }\n\n bool _dfs(TreeNode *node, TreeNode *target, vector<TreeNode *>& path)\n {\n if (node == nullptr) {\n return false;\n }\n\n path.push_back(node);\n\n if (node->val == target->val) {\n return true;\n }\n\n else if (_dfs(node->left, target, path)) {\n return true;\n }\n else if (_dfs(node->right, target, path)) {\n return true;\n }\n\n // not found here\n else {\n path.pop_back();\n return false;\n }\n }\n};",
"memory": "19600"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\n bool path(TreeNode* root, int x, vector< TreeNode*>& arr){\n if(root==NULL) return false;\n arr.push_back(root);\n if(root->val==x){\n return true;\n }\n if(path(root->left,x,arr) || path(root->right,x,arr)) return true;\n arr.pop_back();\n return false; \n }\n\n\n\npublic:\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector< TreeNode*> ans1;\n vector< TreeNode*> ans2;\n path(root,p->val,ans1);\n path(root,q->val,ans2);\n int flag=0;\n int i = ans1.size()-1;\n int j = ans2.size()-1;\n for(i=ans1.size()-1; i>=0; i--){\n for(j=ans2.size()-1; j>=0; j--){\n if(ans1[i]->val==ans2[j]->val){\n flag=1;\n break;\n }\n }\n if(flag==1) break;\n }\n return ans1[i];\n }\n};",
"memory": "19700"
} |
236 | <p>Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.</p>
<p>According to the <a href="https://en.wikipedia.org/wiki/Lowest_common_ancestor" target="_blank">definition of LCA on Wikipedia</a>: “The lowest common ancestor is defined between two nodes <code>p</code> and <code>q</code> as the lowest node in <code>T</code> that has both <code>p</code> and <code>q</code> as descendants (where we allow <b>a node to be a descendant of itself</b>).”</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
<strong>Output:</strong> 3
<strong>Explanation:</strong> The LCA of nodes 5 and 1 is 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/14/binarytree.png" style="width: 200px; height: 190px;" />
<pre>
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
<strong>Output:</strong> 5
<strong>Explanation:</strong> The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> root = [1,2], p = 1, q = 2
<strong>Output:</strong> 1
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
<li><code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code></li>
<li>All <code>Node.val</code> are <strong>unique</strong>.</li>
<li><code>p != q</code></li>
<li><code>p</code> and <code>q</code> will exist 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(int x) : val(x), left(NULL), right(NULL) {}\n * };\n */\nclass Solution {\npublic:\n bool getPath(TreeNode* root, TreeNode* n, vector<TreeNode*> &arr){\n if(root==nullptr) return false;\n arr.push_back(root);\n if(root==n) return true;\n\n if(getPath(root->left, n, arr) || getPath(root->right, n, arr)) return true;\n\n arr.pop_back();\n return false;\n }\n\n vector<TreeNode*> path(TreeNode* root, TreeNode* n){\n vector<TreeNode*> arr;\n if(root==nullptr) return arr;\n getPath(root, n, arr);\n return arr;\n }\n\n TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {\n vector<TreeNode*> v1 = path(root, p);\n vector<TreeNode*> v2 = path(root, q);\n int i=0;\n TreeNode* lca;\n while (i < v1.size() && i < v2.size() && v1[i] == v2[i]) {\n lca = v1[i];\n i++;\n }\n return lca;\n }\n};",
"memory": "19700"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n\n static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, val_str; getline(cin, str) && getline(cin, val_str); cout << '\\n') {\n int val = stoi(val_str);\n stringstream ss(str); ss.ignore();\n bool first = true;\n cout << '[';\n for (int i; ss >> i; ss.ignore()) {\n if (i != val) {\n if (first) {\n cout << i;\n first = false;\n } else {\n cout << ',' << i;\n }\n }\n }\n cout << ']';\n }\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n //we take the indentity of the next node\n node->val = node->next->val;\n //now this node points to the second node after it\n node->next = node->next->next;\n }\n};",
"memory": "7300"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, val_str; getline(cin, str) && getline(cin, val_str); cout << '\\n') {\n int val = stoi(val_str);\n stringstream ss(str); ss.ignore();\n bool first = true;\n cout << '[';\n for (int i; ss >> i; ss.ignore()) {\n if (i != val) {\n if (first) {\n cout << i;\n first = false;\n } else {\n cout << ',' << i;\n }\n }\n }\n cout << ']';\n }\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n //we take the indentity of the next node\n node->val = node->next->val;\n //now this node points to the second node after it\n node->next = node->next->next;\n }\n};",
"memory": "7400"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n \n static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, val_str; getline(cin, str) && getline(cin, val_str); cout << '\\n') {\n int val = stoi(val_str);\n stringstream ss(str); ss.ignore();\n bool first = true;\n cout << '[';\n for (int i; ss >> i; ss.ignore()) {\n if (i != val) {\n if (first) {\n cout << i;\n first = false;\n } else {\n cout << ',' << i;\n }\n }\n }\n cout << ']';\n }\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n ListNode * temp=node->next->next;\n int x=node->next->val;\n node->val=x;\n node->next=temp;\n return ;\n\n \n\n }\n};",
"memory": "7500"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\n static const int __ = [](){\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\nint init = [] {\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n for (string str, val_str; getline(cin, str) && getline(cin, val_str); cout << '\\n') {\n int val = stoi(val_str);\n stringstream ss(str); ss.ignore();\n bool first = true;\n cout << '[';\n for (int i; ss >> i; ss.ignore()) {\n if (i != val) {\n if (first) {\n cout << i;\n first = false;\n } else {\n cout << ',' << i;\n }\n }\n }\n cout << ']';\n }\n exit(0);\n return 0;\n}();\n\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n\n node->val = node->next->val;\n node->next = node->next->next;\n }\n};",
"memory": "7600"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n if(node->next){\n node->val=node->next->val;\n ListNode *temp=node->next;\n node->next=node->next->next;\n delete(temp);\n }\n }\n};",
"memory": "12400"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n if(node == NULL &&node->next==NULL ){\n return;\n }\n \n ListNode * prev = node;\n ListNode * curr = node->next;\n while(curr->next!=NULL){\n prev->val = curr->val;\n prev = curr;\n curr = curr->next;\n }\n prev->val = curr->val;\n prev->next = NULL;\n delete curr;\n }\n};",
"memory": "12500"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n node->val=node->next->val;\n ListNode* temp=node->next;\n node->next=temp->next;\n delete temp;\n \n }\n};",
"memory": "12500"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n ListNode* temp = node;\n while(temp->next!=NULL){\n temp->val = temp->next->val;\n if(temp->next->next==NULL){\n temp->next = NULL;\n return;\n }\n temp = temp->next;\n }\n //temp = NULL;\n }\n};",
"memory": "12600"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n node->val=node->next->val;\n node->next=node->next->next;\n }\n};",
"memory": "12600"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n ListNode *temp=node->next;\n node->val=temp->val;\n node->next=temp->next;\n delete temp;\n }\n};",
"memory": "12700"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 0 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n node->val = node->next->val;\n node->next = node->next->next;\n }\n};",
"memory": "12700"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n node->val = node->next->val;\n node->next = node->next->next;\n }\n};",
"memory": "12800"
} |
237 | <p>There is a singly-linked list <code>head</code> and we want to delete a node <code>node</code> in it.</p>
<p>You are given the node to be deleted <code>node</code>. You will <strong>not be given access</strong> to the first node of <code>head</code>.</p>
<p>All the values of the linked list are <strong>unique</strong>, and it is guaranteed that the given node <code>node</code> is not the last node in the linked list.</p>
<p>Delete the given node. Note that by deleting the node, we do not mean removing it from memory. We mean:</p>
<ul>
<li>The value of the given node should not exist in the linked list.</li>
<li>The number of nodes in the linked list should decrease by one.</li>
<li>All the values before <code>node</code> should be in the same order.</li>
<li>All the values after <code>node</code> should be in the same order.</li>
</ul>
<p><strong>Custom testing:</strong></p>
<ul>
<li>For the input, you should provide the entire linked list <code>head</code> and the node to be given <code>node</code>. <code>node</code> should not be the last node of the list and should be an actual node in the list.</li>
<li>We will build the linked list and pass the node to your function.</li>
<li>The output will be the entire list after calling your function.</li>
</ul>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node1.jpg" style="width: 400px; height: 286px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 5
<strong>Output:</strong> [4,1,9]
<strong>Explanation: </strong>You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
</pre>
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/node2.jpg" style="width: 400px; height: 315px;" />
<pre>
<strong>Input:</strong> head = [4,5,1,9], node = 1
<strong>Output:</strong> [4,5,9]
<strong>Explanation: </strong>You are given the third node with value 1, the linked list should become 4 -> 5 -> 9 after calling your function.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li>The number of the nodes in the given list is in the range <code>[2, 1000]</code>.</li>
<li><code>-1000 <= Node.val <= 1000</code></li>
<li>The value of each node in the list is <strong>unique</strong>.</li>
<li>The <code>node</code> to be deleted is <strong>in the list</strong> and is <strong>not a tail</strong> node.</li>
</ul>
| 1 | {
"code": "/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode(int x) : val(x), next(NULL) {}\n * };\n */\nclass Solution {\npublic:\n void deleteNode(ListNode* node) {\n node->val = node->next->val;\n node->next = node->next->next;\n }\n};",
"memory": "12800"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) { return c >= '0' && c <= '9'; }\n\nstd::array<int, 100000> nums;\nvoid parse_input_and_solve(std::ofstream& out, const std::string& s) {\n const int N = s.size();\n int left = 0;\n int idx = 0;\n while (left < N) {\n if (!isdigit(s[left]) &&\n !(s[left] == '-' && left + 1 < N && isdigit(s[left + 1]))) {\n ++left;\n continue;\n }\n int right = left;\n int value = 0;\n bool negative = false;\n\n if (s[right] == '-') {\n negative = true;\n ++right;\n }\n while (right < N && isdigit(s[right])) {\n value = value * 10 + (s[right] - '0');\n ++right;\n }\n\n if (negative)\n value = -value;\n left = right;\n nums[idx++] = value;\n }\n\n int arr[idx];\n for (int i = 0; i < idx; i++)\n arr[i] = nums[i];\n arr[0] = 1;\n int res = 1;\n for (int i = 0; i < idx - 1; i++) {\n res *= nums[i];\n arr[i + 1] = res;\n }\n res = 1;\n for (int i = idx - 1; i >= 1; i--) {\n res *= nums[i];\n arr[i - 1] *= res;\n }\n\n out << \"[\";\n for (int i = 0; i < idx - 1; i++) {\n out << arr[i] << \",\";\n }\n out << arr[idx - 1] << \"]\\n\";\n}\n\nbool Solve = []() {\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n parse_input_and_solve(out, s);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\n\nclass Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n cin.tie(0), ios::sync_with_stdio(0);\n int s = nums.size();\n if(s == 1){\n return nums;\n }\n if(s == 2){\n int temp = nums[0];\n nums[0] = nums[1];\n nums[1] = temp;\n return nums;\n }\n vector<int> pre(s,0);\n vector<int> suf(s,0);\n pre[0] = nums[0];\n suf[s-1] = nums[s-1];\n for(int i = 1, j = s-2; i < s, j > 0; i++, j--){\n pre[i] = pre[i-1] * nums[i];\n suf[j] = suf[j+1] * nums[j];\n }\n vector<int> res(s,1);\n res[0] = suf[1];\n res[s-1] = pre[s-2];\n for(int i = 1; i < s-1; i++){\n res[i] = pre[i-1] * suf[i+1];\n }\n return res;\n }\n};",
"memory": "9100"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::array<int, 100000> nums;\nvoid parse_input_and_solve(std::ofstream& out, const std::string& s) {\n const int N = s.size();\n int left = 0;\n int idx = 0;\n while (left < N) {\n if (!isdigit(s[left]) && !(s[left] == '-' && left + 1 < N && isdigit(s[left + 1]))) {\n ++left;\n continue;\n }\n int right = left;\n int value = 0;\n bool negative = false;\n\n if (s[right] == '-') {\n negative = true;\n ++right;\n }\n while(right < N && isdigit(s[right])) {\n value = value * 10 + (s[right] - '0');\n ++right;\n }\n\n if(negative) value = -value;\n left = right;\n nums[idx++] = value;\n }\n\n int arr[idx];\n for(int i=0; i<idx; i++) arr[i] = nums[i];\n arr[0] = 1;\n int res = 1;\n for(int i=0; i<idx-1; i++) {\n res *= nums[i];\n arr[i+1] = res;\n }\n res = 1;\n for(int i=idx-1; i>=1; i--) {\n res *= nums[i];\n arr[i-1] *= res;\n }\n\n out << \"[\";\n for(int i=0; i<idx-1; i++) {\n out << arr[i] << \",\";\n }\n out << arr[idx-1] << \"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n parse_input_and_solve(out, s);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n cin.tie(0);\n ios::sync_with_stdio(0);\n int n = nums.size();\n vector<int> answer(n, 1);\n for (int i = 1; i < n; i++) {\n answer[i] = answer[i - 1] * nums[i - 1];\n }\n int temp = 1;\n for (int i = n - 1; i >= 0; i--) {\n answer[i] *= temp;\n temp *= nums[i];\n }\n return answer;\n }\n};",
"memory": "9200"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) { return c >= '0' && c <= '9'; }\n\nstd::array<int, 100000> nums;\nvoid parse_input_and_solve(std::ofstream& out, const std::string& s) {\n const int N = s.size();\n int left = 0;\n int idx = 0;\n while (left < N) {\n if (!isdigit(s[left]) &&\n !(s[left] == '-' && left + 1 < N && isdigit(s[left + 1]))) {\n ++left;\n continue;\n }\n int right = left;\n int value = 0;\n bool negative = false;\n\n if (s[right] == '-') {\n negative = true;\n ++right;\n }\n while (right < N && isdigit(s[right])) {\n value = value * 10 + (s[right] - '0');\n ++right;\n }\n\n if (negative)\n value = -value;\n left = right;\n nums[idx++] = value;\n }\n\n int arr[idx];\n for (int i = 0; i < idx; i++)\n arr[i] = nums[i];\n arr[0] = 1;\n int res = 1;\n for (int i = 0; i < idx - 1; i++) {\n res *= nums[i];\n arr[i + 1] = res;\n }\n res = 1;\n for (int i = idx - 1; i >= 1; i--) {\n res *= nums[i];\n arr[i - 1] *= res;\n }\n\n out << \"[\";\n for (int i = 0; i < idx - 1; i++) {\n out << arr[i] << \",\";\n }\n out << arr[idx - 1] << \"]\\n\";\n}\n\nbool Solve = []() {\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n parse_input_and_solve(out, s);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n cin.tie(0), ios::sync_with_stdio(0);\n\n vector<int> result(nums.size(), 1);\n\n int left_product = 1;\n for (size_t i = 0; i < nums.size(); ++i) {\n result[i] = left_product;\n left_product *= nums[i];\n }\n\n int right_product = 1;\n for (size_t i = nums.size(); i-- > 0;) {\n result[i] *= right_product;\n right_product *= nums[i];\n }\n\n return result;\n }\n};",
"memory": "9300"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = [](){\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) {\n return c >= '0' && c <= '9';\n}\n\nstd::array<int, 100000> nums;\nvoid parse_input_and_solve(std::ofstream& out, const std::string& s) {\n const int N = s.size();\n int left = 0;\n int idx = 0;\n while (left < N) {\n if (!isdigit(s[left]) && !(s[left] == '-' && left + 1 < N && isdigit(s[left + 1]))) {\n ++left;\n continue;\n }\n int right = left;\n int value = 0;\n bool negative = false;\n\n if (s[right] == '-') {\n negative = true;\n ++right;\n }\n while(right < N && isdigit(s[right])) {\n value = value * 10 + (s[right] - '0');\n ++right;\n }\n\n if(negative) value = -value;\n left = right;\n nums[idx++] = value;\n }\n\n int arr[idx];\n for(int i=0; i<idx; i++) arr[i] = nums[i];\n arr[0] = 1;\n int res = 1;\n for(int i=0; i<idx-1; i++) {\n res *= nums[i];\n arr[i+1] = res;\n }\n res = 1;\n for(int i=idx-1; i>=1; i--) {\n res *= nums[i];\n arr[i-1] *= res;\n }\n\n out << \"[\";\n for(int i=0; i<idx-1; i++) {\n out << arr[i] << \",\";\n }\n out << arr[idx-1] << \"]\\n\";\n}\n\nbool Solve = [](){\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n parse_input_and_solve(out, s);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n cin.tie(0);\n ios::sync_with_stdio(0);\n int n = nums.size();\n vector<int> answer(n, 1);\n for (int i = 1; i < n; i++) {\n answer[i] = answer[i - 1] * nums[i - 1];\n }\n int temp = 1;\n for (int i = n - 1; i >= 0; i--) {\n answer[i] *= temp;\n temp *= nums[i];\n }\n return answer;\n }\n};",
"memory": "9400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nstatic const bool Booster = []() {\n std::ios_base::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return true;\n}();\n\ninline bool isdigit(char c) { return c >= '0' && c <= '9'; }\n\nstd::array<int, 100000> nums;\nvoid parse_input_and_solve(std::ofstream& out, const std::string& s) {\n const int N = s.size();\n int left = 0;\n int idx = 0;\n while (left < N) {\n if (!isdigit(s[left]) &&\n !(s[left] == '-' && left + 1 < N && isdigit(s[left + 1]))) {\n ++left;\n continue;\n }\n int right = left;\n int value = 0;\n bool negative = false;\n\n if (s[right] == '-') {\n negative = true;\n ++right;\n }\n while (right < N && isdigit(s[right])) {\n value = value * 10 + (s[right] - '0');\n ++right;\n }\n\n if (negative)\n value = -value;\n left = right;\n nums[idx++] = value;\n }\n\n int arr[idx];\n for (int i = 0; i < idx; i++)\n arr[i] = nums[i];\n arr[0] = 1;\n int res = 1;\n for (int i = 0; i < idx - 1; i++) {\n res *= nums[i];\n arr[i + 1] = res;\n }\n res = 1;\n for (int i = idx - 1; i >= 1; i--) {\n res *= nums[i];\n arr[i - 1] *= res;\n }\n\n out << \"[\";\n for (int i = 0; i < idx - 1; i++) {\n out << arr[i] << \",\";\n }\n out << arr[idx - 1] << \"]\\n\";\n}\n\nbool Solve = []() {\n std::ofstream out(\"user.out\");\n for (std::string s; std::getline(std::cin, s);) {\n parse_input_and_solve(out, s);\n }\n out.flush();\n exit(0);\n return true;\n}();\n\nclass Solution {\n\npublic:\n\n std::vector<int> productExceptSelf(std::vector<int>& nums) {\n \n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1); \n auto posZero = npos; \n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n }\n else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};\n",
"memory": "9500"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "auto init = []() {\n std::ios_base::sync_with_stdio(0);\n std::cin.tie(0);\n std::cout.tie(0);\n return 'C';\n }();\n\nclass Solution {\n\npublic:\n\n std::vector<int> productExceptSelf(std::vector<int>& nums) {\n \n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1); \n auto posZero = npos; \n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n }\n else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};\n",
"memory": "37100"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1);\n auto posZero = npos;\n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n } else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};",
"memory": "37200"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1);\n auto posZero = npos;\n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n if (num) {\n mulNums *= num;\n } else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};",
"memory": "37200"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1);\n auto posZero = npos;\n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n } else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};",
"memory": "37300"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int mulNums = 1;\n static constexpr int npos = -1;\n auto posZero = npos;\n int size = nums.size();\n\n for (int i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n } else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (int i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};",
"memory": "37300"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "auto init = []() {\n std::ios_base::sync_with_stdio(0);\n std::cin.tie(0);\n std::cout.tie(0);\n return 'C';\n }();\n\nclass Solution {\n\npublic:\n\n std::vector<int> productExceptSelf(std::vector<int>& nums) {\n \n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1); \n auto posZero = npos; \n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n }\n else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};\n",
"memory": "37400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "auto init = []() {\n std::ios_base::sync_with_stdio(0);\n std::cin.tie(0);\n std::cout.tie(0);\n return 'C';\n }();\n\nclass Solution {\n\npublic:\n\n std::vector<int> productExceptSelf(std::vector<int>& nums) {\n \n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1); \n auto posZero = npos; \n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n }\n else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};\n",
"memory": "37400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "#pragma GCC optimize(\"O3,unroll-loops\")\n#pragma GCC target(\"avx2,bmi,bmi2,lzcnt,popcnt\")\n\nauto init = []() {\n std::ios_base::sync_with_stdio(0);\n std::cin.tie(0);\n std::cout.tie(0);\n return 'C';\n }();\n\nclass Solution {\n\npublic:\n\n std::vector<int> productExceptSelf(std::vector<int>& nums) {\n \n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1); \n auto posZero = npos; \n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n }\n else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};\n",
"memory": "37500"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "auto init = []() {\n std::ios_base::sync_with_stdio(0);\n std::cin.tie(0);\n std::cout.tie(0);\n return 'C';\n }();\n\nclass Solution {\n\npublic:\n\n std::vector<int> productExceptSelf(std::vector<int>& nums) {\n \n int mulNums = 1;\n static constexpr auto npos = static_cast<size_t>(-1); \n auto posZero = npos; \n auto size = nums.size();\n\n for (size_t i = 0; i < size; ++i) {\n int num = nums[i];\n\n if (num) {\n mulNums *= num;\n }\n else {\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n return std::move(nums);\n }\n posZero = i;\n }\n }\n\n if (posZero != npos) {\n std::fill(nums.begin(), nums.end(), 0);\n nums[posZero] = mulNums;\n return std::move(nums);\n }\n\n for (size_t i = 0; i < size; ++i)\n nums[i] = mulNums / nums[i];\n\n return std::move(nums);\n }\n};\n",
"memory": "37500"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "//Without Using Extra space, TC & SC : O(N) & O(1)\nclass Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int>re(n);\n\n re[0] =1;\n\n for(int i = 1; i < n;i++){//left part \n re[i] = re[i-1]* nums[i-1];\n }\n\n int right = 1;\n\n for(int i = n-1; i>=0; i--){\n re[i] = re[i] * right;\n right *= nums[i];\n }\n\n \n\n return re;\n \n \n \n }\n};",
"memory": "38400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 0 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n // Initialize a vector to store the results and a variable to keep track of the product of elements to the right\n int product = 1;\n vector<int> output(nums.size()); // Create an output vector of the same size as input\n\n // Step 1: Compute the prefix products (product of all elements to the left of the current element)\n for (int i = 0; i < nums.size(); i++) {\n if (i == 0) {\n // For the first element, there's no element to the left, so the prefix product is simply the element itself\n output[i] = nums[i];\n } else {\n // For other elements, the prefix product is the product of the previous prefix product and the current element\n output[i] = output[i - 1] * nums[i];\n }\n }\n\n // Step 2: Compute the suffix products (product of all elements to the right of the current element) and update the output array\n for (int i = nums.size() - 1; i >= 0; i--) {\n // If we are at the last element, there's no element to the right, so we use the prefix product directly\n if (i == nums.size() - 1) {\n output[i] = output[i - 1]; // For the last element, the product is simply the prefix product of the second last element\n } else if (i == 0) {\n // For the first element, we only have the suffix product\n output[i] = product; // Product variable here stores the suffix product for the first element\n } else {\n // For other elements, multiply the current prefix product by the suffix product\n output[i] = product * output[i - 1];\n }\n \n // Update the product variable to include the current element (moving right)\n product *= nums[i];\n }\n\n return output; // Return the result vector\n }\n};\n",
"memory": "38400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> ans(n);\n ans[0] = 1;\n\n for(int i=1;i<n;i++)\n {\n ans[i] = ans[i-1] * nums[i-1];\n }\n\n int prod = 1;\n for(int i = n-2; i>=0; i--)\n {\n prod = prod * nums[i+1];\n ans[i] = ans[i] * prod;\n }\n\n return ans;\n }\n};",
"memory": "38500"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> vec(n, 1); \n \n if (n == 1) {\n return vec;\n }\n \n int temp = 1; \n \n // left \n for (int i = 0; i < n; i++) {\n vec[i] *= temp; \n temp *= nums[i]; \n }\n \n temp = 1; // Reset temp \n \n // right \n for (int i = n - 1; i >= 0; i--) {\n vec[i] *= temp; \n temp *= nums[i]; \n }\n \n return vec;\n }\n};\n",
"memory": "38500"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n const int NUM_VALS{61};\n const int NUM_VALS_OFFSET{30};\n int num_counts[NUM_VALS]{0};\n int powers[NUM_VALS]{0};\n int sol_set[NUM_VALS]{0};\n int n = nums.size();\n vector<int> solutions;\n solutions.resize(n);\n for (int i = 0; i < n; ++i) {\n ++num_counts[nums[i] + NUM_VALS_OFFSET];\n }\n // if 2 zeros, all solutions zero;\n if (num_counts[NUM_VALS_OFFSET] > 1) {\n fill(solutions.begin(), solutions.end(), 0);\n return solutions;\n }\n // calculate each factor\n for (int j = 0; j < NUM_VALS; ++j) {\n if (num_counts[j] == 0)\n continue;\n powers[j] = pow(j - NUM_VALS_OFFSET, num_counts[j]);\n }\n // calc each possible solution for num value\n for (int j = 0; j < NUM_VALS; ++j) {\n if (num_counts[j] == 0)\n continue;\n sol_set[j] = 1;\n if (j != NUM_VALS_OFFSET && num_counts[NUM_VALS_OFFSET] == 1) {\n sol_set[j] = 0;\n continue;\n }\n for (int k = 0; k < NUM_VALS; ++k) {\n if (num_counts[k] == 0)\n continue;\n if (j == k) {\n sol_set[j] *= pow(k - NUM_VALS_OFFSET, num_counts[k] - 1);\n } else {\n sol_set[j] *= powers[k];\n }\n }\n }\n // Set solutions\n for (int i = 0; i < n; ++i) {\n solutions[i] = sol_set[nums[i] + NUM_VALS_OFFSET];\n }\n\n return solutions;\n }\n};",
"memory": "38600"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int i;\n int product;\n const int n = nums.size();\n int result[n];\n \n std::fill_n(result, n, 1);\n\n product = 1;\n for (i = 0; i < n; i++) {\n result[i] *= product;\n product *= nums[i];\n }\n\n product = 1;\n for (i = n-1; i >= 0; i--) {\n result[i] *= product;\n product *= nums[i];\n }\n\n std::vector<int> ans(result, result + n);\n return ans;\n }\n};",
"memory": "38700"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int i;\n int product;\n const int n = nums.size();\n int result[n];\n \n std::fill_n(result, n, 1);\n\n product = 1;\n for (i = 0; i < n; i++) {\n result[i] *= product;\n product *= nums[i];\n }\n\n product = 1;\n for (i = n-1; i >= 0; i--) {\n result[i] *= product;\n product *= nums[i];\n }\n\n std::vector<int> ans(result, result + n);\n return ans;\n }\n};",
"memory": "38800"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> answer(nums.size());\n \n int suffix[nums.size()];\n\n answer[0] = 1;\n suffix[nums.size()-1] = 1;\n\n for(int i = 1; i < nums.size(); i++){\n answer[i] = nums[i-1]*answer[i-1];\n }\n for(int j = nums.size()-2; j >= 0; j--){\n suffix[j] = nums[j+1]*suffix[j+1];\n answer[j] = suffix[j]*answer[j];\n }\n return answer;\n }\n};",
"memory": "38900"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& a) {\n const int size = a.size();\n int l[size];\n l[0] = 1;\n for (int i = 1; i < size; ++i) {\n l[i] = l[i - 1] * a[i - 1];\n }\n for (int i = size - 2, r = 1; i >= 0; --i) {\n r *= a[i + 1];\n l[i] *= r;\n }\n std::copy(l, l + size, a.begin());\n return a;\n }\n};\n",
"memory": "38900"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n\n int n = nums.size();\n vector<int> ans(n,0);\n int prefix[n];\n prefix[0] = nums[0];\n int suffix[n];\n suffix[n-1] = nums[n-1];\n for(int i=1;i<n;i++){\n prefix[i] = prefix[i-1]*nums[i];\n }\n for(int i=n-2;i>=0;i--){\n suffix[i] = suffix[i+1]*nums[i];\n }\n ans[0] = suffix[1];\n for(int i=1;i<n-1;i++){\n ans[i] = prefix[i-1]*suffix[i+1];\n }\n ans[n-1] = prefix[n-2];\n return ans;\n\n }\n // 1 2 3 4\n // 1 2 6 24\n // 24 24 12 4\n};",
"memory": "39000"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n \n int n = nums.size();\n int pref[n + 5], suff[n + 5];\n\n pref[0] = nums[0];\n for (int i = 1; i < n; i++)\n pref[i] = nums[i] * pref[i - 1];\n \n suff[n - 1] = nums[n - 1];\n for (int i = n - 2; i >= 0; i--)\n suff[i] = nums[i] * suff[i + 1];\n\n vector<int> res(n);\n for (int i = 0; i < n; i++) \n {\n if (i == 0)\n res[i] = suff[i + 1];\n else if (i == n - 1)\n res[i] = pref[i - 1];\n else\n res[i] = suff[i + 1] * pref[i - 1];\n }\n \n return res;\n }\n};",
"memory": "39100"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> ans(n, 1);\n vector<int> suffix(n, 1);\n\n for (int i = n - 2; i >= 0; i--) {\n suffix[i] = suffix[i + 1] * nums[i + 1];\n }\n\n for (int i = 1; i < n; i++) {\n ans[i] = ans[i - 1] * nums[i - 1];\n }\n\n for (int i = 0; i < n; i++) {\n ans[i] *= suffix[i];\n }\n\n return ans;\n }\n};\n",
"memory": "39200"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int>left(n, 1);\n vector<int>right(n, 1);\n for(int i = 1; i<n; i++ ){\n left[i]=nums[i-1]*left[i-1];\n }\n \n for(int i = n-2; i>=0; i--){\n right[i]=right[i+1]*nums[i+1];\n }\n \n for(int i = 0; i<n; i++){\n left[i]*=right[i];\n }\n return left;\n \n \n }\n};",
"memory": "39200"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n \n int n = nums.size();\n \n int prepro[n], sufpro[n];\n \n prepro[0] = nums.at(0);\n sufpro[n-1] = nums.at(n-1);\n\n for(int i = 1; i<n; i++) prepro[i] = prepro[i-1]*nums[i];\n for(int i = n-2; i>=0; i--) sufpro[i] = sufpro[i+1]*nums[i];\n\n for(int i = 0; i<n; i++){\n if(i==0) nums[0] = sufpro[1];\n else if(i==n-1) nums[n-1] = prepro[n-2];\n else{\n nums[i] = prepro[i-1] * sufpro[i+1];\n }\n }\n\n return nums;\n }\n};",
"memory": "39300"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> pre(n);\n vector<int> suf(n);\n // prefix product array\n int p = nums[0];\n pre[0] = 1;\n for(int i=1 ; i<n ; i++){\n pre[i] = p;\n p *= nums[i];\n } \n // suffix product array \n p = nums[n-1];\n suf[n-1] = 1;\n for(int i=n-2 ; i>=0 ; i--){\n suf[i] = p;\n p *= nums[i];\n }\n // ab ya toh ek ans array bna k ye kro ya fir sidha pre k andr hi ye kr do as wse bhi baad mein toh woh bematlb ka hi hai naa\n // ans array\n // vector<int> ans(n);\n // for(int i=0 ; i<n ; i++){\n // ans[i] = pre[i]*suf[i];\n // }\n // return ans;\n for(int i=0 ; i<n ; i++){\n pre[i] = pre[i]*suf[i];\n }\n return pre;\n\n }\n};",
"memory": "39300"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> track(nums.size(), 1);\n vector<int> result(nums.size(), 1);\n int currProd = 1;\n for (int i = 0; i < nums.size(); ++i) {\n result[i] *= currProd;\n currProd *= nums[i];\n }\n currProd = 1;\n for (int i = nums.size() - 1; i >= 0; --i) {\n result[i] *= currProd;\n currProd *= nums[i];\n }\n return result;\n }\n};",
"memory": "39400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n\n int n=nums.size();\n vector<int> prefix(n, 1);\n \n\n for(int i=1;i<n;i++){\n\n prefix[i]=prefix[i-1]*nums[i-1];\n }\n\n vector<int>ans(n);\n int factor=1;\n for(int i=n-1;i>=0;i--){\n\n ans[i]=prefix[i]*factor;\n factor*=nums[i];\n \n }\n\n return ans;\n \n }\n};",
"memory": "39400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> pre(n);\n vector<int> suff(n);\n // vector<int> ans(n);\n //prefix product array\n int p =nums[0];\n pre[0] =1;\n for(int i =1;i<n;i++){\n pre[i] = p;\n p *= nums[i];\n }\n //suffix product array\n p = nums[n-1];\n suff[n-1] =1;\n for(int i =n-2;i>=0;i--){\n suff[i] = p;\n p *= nums[i];\n }\n //ans array\n for(int i =0;i<n;i++){\n pre[i] = pre[i]*suff[i];\n }\n return pre;\n }\n};\n// int n = nums.size();\n// int product = 1;\n// int p2 = 1;\n// int noz = 0;\n// for(int i=0;i<n;i++){\n// if(nums[i]==0) noz++;\n// product *= nums[i];\n// if(nums[i] != 0) p2 *= nums[i];\n// }\n// if(noz>1)p2 =0;\n// for(int i=0;i<n;i++){\n// if(nums[i]==0) nums[i] = p2;\n// else nums[i] = product/nums[i];\n// }\n// return nums;",
"memory": "39500"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int>left(n, 1);\n vector<int>right(n, 1);\n for(int i = 1; i<n; i++ ){\n left[i]=nums[i-1]*left[i-1];\n }\n \n for(int i = n-2; i>=0; i--){\n right[i]=right[i+1]*nums[i+1];\n }\n \n for(int i = 0; i<n; i++){\n left[i]*=right[i];\n }\n return left;\n \n \n }\n};",
"memory": "39500"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n\n int n= nums.size();\n\n vector<int> leftProd(n), rightProd(n);\n\n leftProd[0]= 1, rightProd[n-1]= 1;\n\n for (int i=1; i<n; i++) {\n leftProd[i]= nums[i-1]*leftProd[i-1];\n }\n\n for (int i=n-2; i>=0; i--) {\n rightProd[i]= nums[i+1]*rightProd[i+1];\n }\n\n for (int i=0; i<n; i++) {\n rightProd[i] *= leftProd[i];\n }\n\n return rightProd;\n \n }\n};",
"memory": "39600"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n //[2,3,4]\n //[2,6,24]\n //[24,12,4]\n //[12,8,6]\n\n //[1,2,6]\n //[12,4,1]\n\n vector<int> ans;\n int prefix = nums[0]; \n int postfix = nums[nums.size() - 1];\n\n //ans[i] = prefix[i-1]*postfix[i+1]\n\n ans.push_back(1);\n\n //fill the answer array with the values from prefix\n for(int i = 1; i < nums.size(); i++){\n ans.push_back(prefix);\n prefix *= nums[i];\n }\n\n for(int i = nums.size() - 2; i >= 0; i--){\n ans[i] *= postfix;\n postfix *= nums[i];\n }\n\n return ans;\n }\n};",
"memory": "39700"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> out;\n long long pro=1;\n for(int i=0;i<nums.size();i++){\n pro*=nums[i];\n out.push_back(pro);\n }\n pro=1;\n for(int i=nums.size()-1;i>0;i--){\n out[i]=out[i-1]*pro;\n pro*=nums[i];\n \n }\n out[0]=pro;\n return out;\n }\n};",
"memory": "39700"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector <int> answer;\n int product = 1;\n for(int i = 0; i < nums.size(); i++){\n product *=nums[i] ;\n answer.push_back(product);\n }\n product = 1;\n for(int i = nums.size()-1; i > 0; i--){\n answer[i]= answer[i-1]*product;\n product *= nums[i];\n }\n answer[0] = product;\n return answer;\n }\n};",
"memory": "39800"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) \n {\n int prod=1;\n int total_zeros=0;\n vector<int> ans;\n for(auto it:nums)\n {\n //prod*=it;\n if(it==0)\n {\n total_zeros++;\n }\n else\n {\n prod*=it;\n }\n }\n \n //int total_zeros=0;\n int i=0;\n for(auto it:nums)\n {\n if(total_zeros>1)\n {\n ans.push_back(0);\n }\n else if(total_zeros==1)\n {\n if(it==0)\n {\n ans.push_back(prod);\n }\n else\n {\n ans.push_back(0);\n }\n }\n else\n {\n ans.push_back(prod/it);\n }\n \n \n }\n \n return ans;\n\n\n\n\n \n }\n};",
"memory": "39800"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> ans;\n \n int zero = 0;\n int product = 1;\n for(auto x : nums){\n if(x == 0){\n zero = zero + 1;\n }else{\n product = product * x;\n }\n }\n \n for(auto x : nums){\n if(x == 0){\n if(zero != 1){\n ans.push_back(0);\n }else{\n ans.push_back(product);\n }\n }else{\n if(zero == 0){\n ans.push_back(product/x);\n }else{\n ans.push_back(0);\n }\n }\n }\n \n return ans;\n }\n};",
"memory": "39900"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int zeroCount = 0;\n int product = 1;\n int productExceptZero = 1;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==0)\n {\n zeroCount++;\n }\n else\n {\n productExceptZero*=nums[i];\n }\n product*=nums[i];\n }\n vector<int> res;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]==0)\n {\n if(zeroCount>1)\n {\n res.push_back(0);\n }\n else\n {\n res.push_back(productExceptZero);\n }\n }\n else\n {\n res.push_back(product/nums[i]);\n }\n }\n return res;\n }\n};",
"memory": "39900"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> ans;\n int zero_index = -1;\n int product = 1;\n int moreThanOne = 0;\n for(int i = 0; i < nums.size(); ++i) {\n if(nums[i] == 0) {\n zero_index = i;\n moreThanOne ++;\n } else {\n product *= nums[i];\n }\n }\n for(int i = 0; i < nums.size(); ++i) {\n if(zero_index != -1) {\n if (moreThanOne > 1){\n ans.push_back(0);\n } else {\n if(i == zero_index) {\n ans.push_back(product);\n } else {\n ans.push_back(0);\n }\n }\n } else {\n ans.push_back(product/nums[i]);\n }\n } \n return ans;\n }\n};",
"memory": "40000"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector <int> answer;\n int product = 1;\n for(int i = 0; i < nums.size(); i++){\n product *=nums[i] ;\n answer.push_back(product);\n }\n product = 1;\n for(int i = nums.size()-1; i > 0; i--){\n answer[i]= answer[i-1]*product;\n product *= nums[i];\n }\n answer[0] = product;\n return answer;\n }\n};",
"memory": "40000"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n=nums.size();\n vector <int> pre(n,1);\n vector <int> suf(n,1);\n for(int i=1;i<n;i++)\n {\n pre[i]=nums[i-1]*pre[i-1];\n }\n for(int i=n-2;i>=0;i--)\n {\n suf[i]=nums[i+1]*suf[i+1];\n }\n\n for(int i=0;i<n;i++)\n {\n nums[i]=pre[i]*suf[i];\n }\n return nums;\n \n }\n};",
"memory": "40100"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> pre(n),suf(n);\n pre[0]=nums[0];\n suf[n-1]=nums[n-1];\n for (int i=1;i<n;i++){\n pre[i] = pre[i-1]*nums[i];\n }\n for (int i=n-2;i>=0;i--){\n suf[i]=nums[i]*suf[i+1];\n }\n vector<int> ans(n);\n ans[0]=suf[1];\n ans[n-1]=pre[n-2];\n for (int i=1;i<n-1;i++){\n ans[i] = pre[i-1]*suf[i+1];\n }\n return ans;\n \n\n }\n};",
"memory": "40100"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> pre(n);\n vector<int> suf(n);\n \n // Pre\n pre[0] = 1;\n int p=nums[0];\n for(int i=1 ; i<n ; i++){\n pre[i] = p;\n p *= nums[i];\n }\n\n // Suf\n suf[n-1] = 1;\n p=nums[n-1];\n for(int i=n-2 ; i>=0 ; i--){\n suf[i] = p;\n p *= nums[i];\n } \n\n for(int i=0 ; i<n ; i++){\n nums[i] = pre[i]*suf[i];\n }\n return nums;\n \n }\n};",
"memory": "40200"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n=nums.size();\n vector<int>pre(n);\n vector<int>suf(n);\n pre[0]=nums[0];\n suf[n-1]=nums[n-1];\n for(int i=1;i<nums.size();i++){\n pre[i]=nums[i]*pre[i-1];\n // suf[n-i-1]=nums[n-i-1]*suf[n-i];\n }\n for (int i = n-2; i >= 0; i--) {\n suf[i] = nums[i] * suf[i+1];\n }\n \n for(int i=0;i<n;i++){\n if(i==0)nums[i]=suf[1];\n else if(i==n-1)nums[i]=pre[n-2];\n else nums[i]=pre[i-1]*suf[i+1];\n }\n return nums;\n }\n};",
"memory": "40200"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n=nums.size();\n vector<int> ans(n);\n vector<int> left(n);\n vector<int> right(n);\n int product=1;\n for(int i=0;i<n;i++){\n if(i==0){\n left[i]=1;\n continue;\n }\n product=product*nums[i-1];\n left[i]=product;\n }\n product=1;\n for(int i=n-1;i>=0;i--){\n if(i==n-1){\n right[i]=1;\n continue;\n }\n product=product*nums[i+1];\n right[i]=product;\n\n }\n\n for(int i=0;i<n;i++){\n ans[i]=(left[i]*right[i]);\n }\n\n return ans;\n }\n};",
"memory": "40300"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int>result(n);\n vector<int>left(n);\n vector<int>right(n);\n left[0]=1;\n right[n-1]=1;\n for(int i =1;i<n;i++){\n left[i]=nums[i-1]*left[i-1];\n }\n for(int i=n-2;i>=0;i--){\n right[i]=nums[i+1]*right[i+1];\n }\n for(int i =0;i<n;i++){\n result[i]=right[i]*left[i];\n }\n return result;\n }\n};\n\n\n",
"memory": "40300"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n int n = nums.size();\n vector<int> leftToRight(n, 1);\n vector<int> rightToLeft(n, 1);\n for(int index = 1; index < n; index++)\n leftToRight[index] = leftToRight[index - 1] * nums[index - 1];\n for(int index = n - 2; index >= 0; index--)\n rightToLeft[index] = rightToLeft[index + 1] * nums[index + 1];\n vector<int> result(n);\n for(int index = 0; index < n; index++)\n result[index] = leftToRight[index] * rightToLeft[index];\n return result;\n }\n};\n",
"memory": "40400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 2 | {
"code": "class Solution {\n public:\n vector<int> productExceptSelf(vector<int>& nums) {\n const int n = nums.size();\n vector<int> ans(n); // Can also use `nums` as the ans array.\n vector<int> prefix(n, 1); // prefix product\n vector<int> suffix(n, 1); // suffix product\n\n for (int i = 1; i < n; ++i)\n prefix[i] = prefix[i - 1] * nums[i - 1];\n\n for (int i = n - 2; i >= 0; --i)\n suffix[i] = suffix[i + 1] * nums[i + 1];\n\n for (int i = 0; i < n; ++i)\n ans[i] = prefix[i] * suffix[i];\n\n return ans;\n }\n};",
"memory": "40400"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n if (nums.size() == 2) {\n return {nums[1], nums[0]};\n }\n\n vector<int> preffix(nums.size(), 0);\n vector<int> suffix(nums.size(), 0);\n\n long int total = 1;\n for (int i = 0; i != nums.size(); ++i) {\n total *= nums[i];\n preffix[i] = total;\n }\n\n total = 1;\n for (int i = nums.size() - 1; i != -1; --i) {\n total *= nums[i];\n suffix[i] = total;\n }\n\n vector<int> result(nums.size(), 0);\n for (int i = 1; i != nums.size() - 1; ++i) {\n result[i] = preffix[i-1] * suffix[i+1];\n }\n result[0] = suffix[1];\n result[nums.size() - 1] = preffix[nums.size() - 2];\n\n return result;\n }\n};",
"memory": "40500"
} |
238 | <p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>
<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>
<p>You must write an algorithm that runs in <code>O(n)</code> time and without using the division operation.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>-30 <= nums[i] <= 30</code></li>
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>
<p> </p>
<p><strong>Follow up:</strong> Can you solve the problem in <code>O(1)</code> extra space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> productExceptSelf(vector<int>& nums) {\n vector<int> left (nums.size());\n vector<int> right (nums.size());\n\n std::partial_sum(nums.begin(), nums.end(), left.begin(), std::multiplies<>{});\n std::partial_sum(nums.rbegin(), nums.rend(), right.rbegin(), std::multiplies<>{});\n\n vector<int> result(nums.size());\n for (int i = 0; i < nums.size(); i++)\n {\n int prefix = i == 0 ? 1 : left[i - 1];\n int suffix = i == nums.size() - 1 ? 1 : right[i + 1];\n result[i] = prefix * suffix;\n }\n\n return result;\n }\n};",
"memory": "40500"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.