id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int DFS(TreeNode*root,int &max_without){\n if(!root->right && !root->left){\n max_without = 0;\n return root->val;\n }\n int max_without_left = 0,max_without_right = 0;;\n if(root->left){\n max_without += DFS(root->left,max_without_left);\n }\n if(root->right){\n max_without += DFS(root->right,max_without_right);\n }\n return max(max_without,max_without_left + max_without_right + root->val);\n }\n int rob(TreeNode* root) {\n //max_root = max( max_without_left + max_without_right + root->val, max_without_root);\n //max_without_root = max_left + max_right.\n int max_without=0;\n return DFS(root,max_without);\n \n }\n};", "memory": "17640" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int dfsSize(TreeNode* root)\n {\n if (!root) return 0;\n\n int left = dfsSize(root->left);\n int right = dfsSize(root->right);\n\n return left + right + 1;\n }\n\n int dfs(TreeNode* root, vector<vector<int>> &dp, int &c)\n {\n if (!root) return 0;\n\n int node = c;\n dp[node][1] = root->val;\n\n if (root->left)\n {\n int child = dfs(root->left, dp, ++c);\n\n dp[node][1] += dp[child][0];\n dp[node][0] = max(dp[child][0], dp[child][1]);\n }\n\n if (root->right)\n {\n int child = dfs(root->right, dp, ++c);\n\n dp[node][1] += dp[child][0];\n dp[node][0] += max(dp[child][0], dp[child][1]);\n }\n\n return node;\n }\n\n int rob(TreeNode* root) \n {\n int n = dfsSize(root);\n\n vector<vector<int>> dp(n + 1, vector<int> (2, 0));\n\n n = 0; \n dfs(root, dp, n);\n\n return max(dp[0][0], dp[0][1]);\n }\n};", "memory": "17851" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n pair<int,int> dfs(TreeNode* src)\n {\n if(src == nullptr)\n {\n return {0,0};\n }\n int value = src->val;\n pair<int,int> p1 = dfs(src->left);\n pair<int,int> p2 = dfs(src->right);\n pair<int,int> ans = {value + p1.second + p2.second, max({p1.first + p2.first,p1.second + p2.second,p1.first + p2.second, p1.second + p2.first})};\n return ans;\n }\n int getAns(TreeNode* root){\n if(root==NULL) return 0;\n // int pick=root->val;\n pair<int,int> p = dfs(root);\n return max(p.first,p.second);\n\n \n // if(root->left) pick+=getAns(root->left->left)+getAns(root->left->right);\n\n // if(root->right) pick+=getAns(root->right->left)+getAns(root->right->right); \n \n // int notpick= getAns(root->left)+getAns(root->right);\n\n // return max(pick,notpick);\n }\n int rob(TreeNode* root) {\n return getAns(root);\n }\n};", "memory": "18063" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n int getValue(TreeNode*root){\n if(root==NULL){\n return 0;\n }\n return root->val;\n }\n \n void CopyTree(TreeNode*root,TreeNode*&u,TreeNode*&v){\n if(root!=NULL){\n \n if(root->left!=NULL){\n u->left = new TreeNode(root->left->val);\n v->left = new TreeNode(root->left->val);\n CopyTree(root->left,u->left,v->left);\n }\n if(root->right!=NULL){\n u->right = new TreeNode(root->right->val);\n v->right = new TreeNode(root->right->val);\n CopyTree(root->right,u->right,v->right);\n }\n }\n }\n int Key(TreeNode*root){\n if(root == NULL){\n return 0;\n }\n return root->val;\n }\n void DFS(TreeNode*root,TreeNode*u,TreeNode*v){\n if(root!=NULL){\n DFS(root->left,u->left,v->left);\n DFS(root->right,u->right,v->right);\n u->val = root->val + Key(v->left)+Key(v->right);\n v->val= max(Key(u->left),Key(v->left))+max(Key(u->right),Key(v->right));\n }\n \n }\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n TreeNode*u = new TreeNode(root->val);\n TreeNode*v = new TreeNode(root->val);\n CopyTree(root,u,v);\n DFS(root,u,v);\n return max(u->val,v->val);\n }\n};", "memory": "18274" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n int getValue(TreeNode*root){\n if(root==NULL){\n return 0;\n }\n return root->val;\n }\n \n void CopyTree(TreeNode*root,TreeNode*&u,TreeNode*&v){\n if(root!=NULL){\n \n if(root->left!=NULL){\n u->left = new TreeNode(root->left->val);\n v->left = new TreeNode(root->left->val);\n CopyTree(root->left,u->left,v->left);\n }\n if(root->right!=NULL){\n u->right = new TreeNode(root->right->val);\n v->right = new TreeNode(root->right->val);\n CopyTree(root->right,u->right,v->right);\n }\n }\n }\n int Key(TreeNode*root){\n if(root == NULL){\n return 0;\n }\n return root->val;\n }\n void DFS(TreeNode*root,TreeNode*u,TreeNode*v){\n if(root!=NULL){\n DFS(root->left,u->left,v->left);\n DFS(root->right,u->right,v->right);\n u->val = root->val + Key(v->left)+Key(v->right);\n v->val= max(Key(u->left),Key(v->left))+max(Key(u->right),Key(v->right));\n }\n \n }\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n TreeNode*u = new TreeNode(root->val);\n TreeNode*v = new TreeNode(root->val);\n CopyTree(root,u,v);\n DFS(root,u,v);\n return max(u->val,v->val);\n }\n};", "memory": "18274" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n int ans_result = 0;\n\n node_stack.push_back(root);\n int scan_size = 1;\n\n int cur_level_num = 0;\n int start_index = 0;\n int end_index = 0;\n\n // Construct rec map firstly\n while(1){\n if( scan_size == 0 ){\n break;\n }\n\n cur_level_num = 0;\n start_index = node_stack.size() - scan_size;\n end_index = node_stack.size();\n\n for(int scan_index = start_index;scan_index < end_index;scan_index++){\n if(node_stack[scan_index]->left != nullptr){\n node_stack.push_back(node_stack[scan_index]->left);\n cur_level_num++;\n }\n\n if(node_stack[scan_index]->right != nullptr){\n node_stack.push_back(node_stack[scan_index]->right);\n cur_level_num++;\n }\n }\n\n scan_size = cur_level_num;\n\n }\n\n \n start_index = node_stack.size() - 1;\n end_index = 0;\n\n for(int scan_index = start_index;scan_index >= 0;scan_index--){\n \n get_max(node_stack[scan_index],false,ans_result);\n }\n \n //get_max(root,false,ans_result);\n\n return ans_result;\n \n }\n\n void get_max(TreeNode* root,bool is_skip_self,int &sub_max){\n int left_max_saved = -1;\n int right_max_saved = -1;\n\n if( root == nullptr ){\n sub_max = 0;\n return;\n }\n\n if( root->left != nullptr){\n left_max_saved = root->left->val;\n }\n\n if( root->right != nullptr){\n right_max_saved = root->right->val;\n }\n\n if(is_skip_self){\n if( root->left == nullptr ){\n if( root->right == nullptr ){\n sub_max = 0;\n \n return;\n }\n }\n }\n\n if(is_skip_self){\n if( root->right == nullptr ){\n if( left_max_saved != -1 ){\n sub_max = left_max_saved;\n return;\n }\n#if 0\n int left_max = 0;\n get_max(root->left,false,left_max);\n sub_max = left_max;\n#endif\n sub_max = root->right->val;\n \n return;\n }\n }\n\n if(is_skip_self){\n if( root->left == nullptr ){\n if( right_max_saved != -1 ){\n sub_max = right_max_saved;\n return;\n }\n#if 0\n int right_max = 0;\n get_max(root->right,false,right_max);\n sub_max = right_max;\n\n#endif \n sub_max = root->left->val;\n return;\n\n }\n }\n\n if( root->left == nullptr ){\n if( root->right == nullptr ){\n sub_max = root->val;\n return;\n }\n }\n\n \n\n int root_not_selected = 0;\n\n if( root->left == nullptr ){\n root_not_selected = root->right->val;\n \n\n }else if( root->right == nullptr ){\n root_not_selected = root->left->val;\n \n \n }else{\n root_not_selected = root->left->val + root->right->val;\n }\n\n // Root not selected \n if(is_skip_self){\n sub_max = root_not_selected;\n return;\n }\n\n // Root selected\n int root_selected = root->val;\n\n {\n int left_max = 0;\n if( root->left != nullptr ){\n get_max(root->left,true,left_max);\n }\n \n int right_max = 0;\n if( root->right != nullptr ){\n get_max(root->right,true,right_max);\n }\n \n root_selected = root_selected + left_max + right_max;\n }\n \n sub_max = max(root_selected,root_not_selected);\n\n root->val = sub_max;\n return;\n }\n\n\nprivate:\n \n vector<TreeNode*> node_stack;\n};", "memory": "18485" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*, int> dpCalcMap;\n\n\n int rob(TreeNode* root) {\n if (dpCalcMap.contains(root)) {\n return dpCalcMap.at(root);\n }\n\n if (root == NULL) {\n return 0;\n }\n\n if (root->left == NULL && root->right == NULL) {\n return root->val;\n }\n\n int maxValFromChildOfLeft = 0;\n if (root->left != NULL) {\n maxValFromChildOfLeft += rob(root->left->left) + rob(root->left->right);\n }\n int maxValFromChildOfRight = 0;\n if (root->right != NULL) {\n maxValFromChildOfRight = rob(root->right->left) + rob(root->right->right);\n }\n dpCalcMap.try_emplace(root, max(root->val + maxValFromChildOfLeft + maxValFromChildOfRight, \n rob(root->left)+ rob(root->right)));\n\n return dpCalcMap.at(root);\n }\n};", "memory": "18696" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int dp[10005][2];\n map<int, int>mp;\n int count1 = 0;\n int DP(TreeNode* root, int node, int takenParent)\n {\n if(dp[node][takenParent]!=-1) return dp[node][takenParent];\n int val = 0, val1 = 0;\n if(!takenParent){\n val = mp[root->val];\n if(root->left != NULL) val += DP(root->left, root->left->val, 1);\n if(root->right != NULL) val += DP(root->right, root->right->val, 1);\n }\n if(root->left != NULL) val1 += DP(root->left, root->left->val, 0);\n if(root->right != NULL) val1 += DP(root->right, root->right->val, 0);\n return dp[node][takenParent] = max(val, val1);\n }\n void DFS(TreeNode* root)\n {\n if(root==NULL) return;\n mp[count1] = root->val;\n root->val = count1;\n count1 += 1;\n DFS(root->left);\n DFS(root->right);\n }\n\n int rob(TreeNode* root) {\n DFS(root);\n memset(dp, -1, sizeof(dp));\n return DP(root, 0, 0);\n }\n};", "memory": "18908" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int dp[10000][2];\n map<int, int>mp;\n int count1 = 0;\n int DP(TreeNode* root, int node, int takenParent)\n {\n if(dp[node][takenParent]!=-1) return dp[node][takenParent];\n int val = 0, val1 = 0;\n if(!takenParent){\n val = mp[root->val];\n if(root->left != NULL) val += DP(root->left, root->left->val, 1);\n if(root->right != NULL) val += DP(root->right, root->right->val, 1);\n }\n if(root->left != NULL) val1 += DP(root->left, root->left->val, 0);\n if(root->right != NULL) val1 += DP(root->right, root->right->val, 0);\n return dp[node][takenParent] = max(val, val1);\n }\n void DFS(TreeNode* root)\n {\n if(root==NULL) return;\n mp[count1] = root->val;\n dp[count1][0] = dp[count1][1] = -1;\n root->val = count1;\n count1 += 1;\n DFS(root->left);\n DFS(root->right);\n }\n\n int rob(TreeNode* root) {\n DFS(root);\n return DP(root, 0, 0);\n }\n};", "memory": "19119" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n map<TreeNode*,int>mpp;\npublic:\n int rob(TreeNode* root) {\n if(root==NULL) return NULL;\n if(mpp.find(root)!=mpp.end()) return mpp[root];\n int val=0;\n if(root->left!=NULL) val+=(rob(root->left->left)+rob(root->left->right));\n if(root->right!=NULL) val+=(rob(root->right->left)+rob(root->right->right));\n mpp.insert({root, max(root->val + val, rob(root->left) + rob(root->right))});\n\n return mpp[root]; \n }\n};", "memory": "19330" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n set<TreeNode* >dp;\n int FUNC(TreeNode* R){\n if(R==NULL){\n return 0;\n }\n if(dp.find(R)!=dp.end()){\n return R->val;\n }\n int Lo=0;\n int Ro=0;\n if(R->left!=NULL){\n Lo=FUNC(R->left->left)+FUNC(R->left->right);\n }\n if(R->right!=NULL){\n Ro=FUNC(R->right->left)+FUNC(R->right->right);\n }\n int P=Lo+Ro+R->val;\n int NP=FUNC(R->left)+FUNC(R->right);\n R->val=max(P,NP);\n dp.insert(R);\n return R->val;\n }\n int rob(TreeNode* R) {\n return FUNC(R);\n }\n};\n", "memory": "19541" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int _rob(TreeNode* node, unordered_map<TreeNode*, int> &memo) {\n // Base cases\n if (!node) {\n return 0;\n }\n if (!(node->left || node->right)) {\n return node->val; \n }\n\n // Already found\n auto it = memo.find(node);\n if (it != memo.end()) { \n return it->second;\n }\n\n // Subproblems\n int grandchildrenVal = 0; \n grandchildrenVal += node->left ? _rob(node->left->left, memo) : 0; \n grandchildrenVal += node->left ? _rob(node->left->right, memo) : 0; \n grandchildrenVal += node->right ? _rob(node->right->left, memo) : 0; \n grandchildrenVal += node->right ? _rob(node->right->right, memo) : 0; \n\n int useSelf = node->val + grandchildrenVal;\n int skipSelf = _rob(node->left, memo) + _rob(node->right, memo); \n int optimum = max(useSelf, skipSelf); \n\n memo.insert({node, optimum}); \n return optimum;\n }\npublic:\n int rob(TreeNode* root) {\n // Recursive pull DP\n // Use hashMap for memoization: TreeNode* -> payoff\n // Subproblem: maximum payoff attainable in subtree T\n // Relate: dp[T] = max(dp[rootT left Subtree] + dp[rootT right Subtree], \n // cur + sum(dp[rootT left grandchild trees]) + sum(dp[rootT right grandchild trees]))\n // Topological order: From leaf nodes to root node\n // Base case: Leaf nodes and null\n unordered_map<TreeNode*, int> memo;\n return _rob(root, memo);\n }\n};", "memory": "19753" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int robchild(TreeNode* root,TreeNode* head){\n if(root==NULL){\n return 0;\n }\n else if(root->left==NULL && root->right==NULL){\n return 0;\n }\n else if(root->left==NULL){\n return robbing(root->right,head->right);\n }\n else if(root->right==NULL){\n return robbing(root->left,head->left);\n }\n else{\n return robbing(root->left,head->left)+robbing(root->right,head->right);\n }\n }\n int robbing(TreeNode* root,TreeNode* head){\n if(root==NULL){\n return 0;\n }\n else if(root->left==NULL && root->right==NULL){\n head->val=root->val;\n return root->val;\n }\n if(head->val!=-1){\n return head->val;\n }\n int temp1=robbing(root->left,head->left)+robbing(root->right,head->right);\n int temp2=root->val+robchild(root->left,head->left)+robchild(root->right,head->right);\n if(temp1>=temp2){\n head->val=temp1;\n return temp1;\n }\n else{\n head->val=temp2;\n return temp2;\n }\n }\n TreeNode* robber(TreeNode* root) {\n if(root==NULL){\n return NULL;\n }\n else{\n TreeNode* head=new TreeNode(-1);\n head->left=robber(root->left);\n head->right=robber(root->right);\n return head;\n }\n }\n int rob(TreeNode* root){\n TreeNode* head=robber(root);\n return robbing(root,head);\n }\n};", "memory": "19964" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n int rob(const TreeNode* const root) {\n return BuildRobbingTree_r(root)->val;\n }\n\n TreeNode* BuildRobbingTree_r(const TreeNode* const node)\n {\n if(node == nullptr)\n return nullptr;\n\n TreeNode* const left= BuildRobbingTree_r(node->left);\n TreeNode* const right= BuildRobbingTree_r(node->right);\n\n int rob_this_node_res= 0;\n {\n rob_this_node_res+= node->val;\n if(left != nullptr)\n {\n if(left->left != nullptr)\n rob_this_node_res+= left->left->val;\n if(left->right != nullptr)\n rob_this_node_res+= left->right->val;\n }\n if(right != nullptr)\n {\n if(right->left != nullptr)\n rob_this_node_res+= right->left->val;\n if(right->right != nullptr)\n rob_this_node_res+= right->right->val;\n }\n }\n\n int leave_this_node_res= 0;\n {\n if(left != nullptr)\n leave_this_node_res+= left->val;\n if(right != nullptr)\n leave_this_node_res+= right->val;\n }\n\n const int max_profit= std::max(rob_this_node_res, leave_this_node_res);\n\n return new TreeNode(max_profit, left, right);\n }\n};", "memory": "20175" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),\n * right(right) {}\n * };\n */\n\nclass Solution {\npublic:\n int rob(const TreeNode* const root) {\n return BuildRobbingTree_r(root)->val;\n }\n\n TreeNode* BuildRobbingTree_r(const TreeNode* const node) {\n if (node == nullptr)\n return nullptr;\n\n TreeNode* const left = BuildRobbingTree_r(node->left);\n TreeNode* const right = BuildRobbingTree_r(node->right);\n\n int rob_this_node_res = node->val;\n int leave_this_node_res = 0;\n\n if (left != nullptr) {\n leave_this_node_res += left->val;\n if (left->left != nullptr)\n rob_this_node_res += left->left->val;\n if (left->right != nullptr)\n rob_this_node_res += left->right->val;\n }\n if (right != nullptr) {\n leave_this_node_res += right->val;\n if (right->left != nullptr)\n rob_this_node_res += right->left->val;\n if (right->right != nullptr)\n rob_this_node_res += right->right->val;\n }\n\n const int max_profit = std::max(rob_this_node_res, leave_this_node_res);\n\n return new TreeNode(max_profit, left, right);\n }\n};", "memory": "20175" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int f(TreeNode* root, unordered_map<TreeNode*, int> &map){\n if(!root){\n return 0;\n }\n if(!root->left and !root->right){\n return root->val;\n }\n if(map.count(root)) return map[root];\n int pick = 0;\n int notPick = 0;\n pick = root->val;\n if(root->left){\n pick += f(root->left->left, map) + f(root->left->right, map);\n }\n if(root->right){\n pick += f(root->right->left, map) + f(root->right->right, map);\n }\n notPick = 0 + f(root->left, map) + f(root->right, map);\n return map[root] = max(pick, notPick);\n }\n\n int rob(TreeNode* root) {\n if(!root){\n return 0;\n }\n if(!root->left and !root->right){\n return root->val;\n }\n unordered_map<TreeNode*, int> map;\n return f(root, map);\n }\n};", "memory": "20386" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // int recursion(TreeNode* root) {\n // if(root == NULL) return 0;\n\n // if(root->left == NULL && root->right == NULL) return root->val;\n\n // //include\n // int include = root->val;\n // if(root->left != NULL) {\n // int left = 0, right = 0;\n // if(root->left->left != NULL) {\n // left = recursion(root->left->left);\n // }\n // if(root->left->right != NULL) {\n // right = recursion(root->left->right);\n // }\n // include += left + right;\n // }\n // if(root->right != NULL) {\n // int left = 0, right = 0;\n // if(root->right->left != NULL) {\n // left = recursion(root->right->left);\n // }\n // if(root->right->right != NULL) {\n // right = recursion(root->right->right);\n // }\n // include += left + right;\n // }\n\n // //exclude\n // int exclude = 0;\n // if(root->left != NULL) {\n // exclude += recursion(root->left);\n // }\n // if(root->right != NULL) {\n // exclude += recursion(root->right);\n // }\n // int sum = max(include, exclude);\n // return sum;\n // }\n\n int memoization(TreeNode* root, unordered_map<TreeNode*, int>& mp) {\n if(root == NULL) return 0;\n if(root->left == NULL && root->right == NULL) return root->val;\n if(mp.find(root) != mp.end()) return mp[root];\n\n //include\n int include = root->val;\n if(root->left != NULL && root->left->left != NULL) {\n include += memoization(root->left->left, mp);\n }\n if(root->left != NULL && root->left->right != NULL) {\n include += memoization(root->left->right, mp);\n }\n if(root->right != NULL && root->right->left != NULL) {\n include += memoization(root->right->left, mp);\n }\n if(root->right != NULL && root->right->right != NULL) {\n include += memoization(root->right->right, mp);\n }\n\n //exclude\n int exclude = 0;\n if(root->left != NULL) {\n exclude += memoization(root->left, mp);\n }\n if(root->right != NULL) {\n exclude += memoization(root->right, mp);\n }\n return mp[root] = max(include, exclude);\n }\n\n int rob(TreeNode* root) {\n // return recursion(root);\n\n unordered_map<TreeNode*, int> mp;\n return memoization(root, mp);\n }\n};", "memory": "20598" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int maxAmount(TreeNode *root,unordered_map <TreeNode*,int> &dp) {\n if(root == NULL)\n return 0;\n if(root->left == NULL && root->right == NULL)\n return root->val;\n // either pick\n if(dp.find(root) != dp.end())\n return dp[root];\n int pick = root->val;\n if(root -> right != NULL)\n pick = pick + maxAmount(root->right->right,dp);\n if(root -> right != NULL)\n pick = pick + maxAmount(root->right->left,dp);\n if(root->left != NULL)\n pick = pick + maxAmount(root->left->right,dp);\n if(root->left != NULL)\n pick = pick + maxAmount(root->left->left,dp);\n\n int notPick = maxAmount(root->right,dp) + maxAmount(root->left,dp);\n\n return dp[root] = max(pick , notPick);\n }\n int rob(TreeNode* root) {\n if(root == NULL)\n return 0;\n unordered_map <TreeNode*,int> dp;\n return maxAmount(root,dp);\n }\n};", "memory": "20809" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // int recursion(TreeNode* root) {\n // if(root == NULL) return 0;\n\n // if(root->left == NULL && root->right == NULL) return root->val;\n\n // //include\n // int include = root->val;\n // if(root->left != NULL) {\n // int left = 0, right = 0;\n // if(root->left->left != NULL) {\n // left = recursion(root->left->left);\n // }\n // if(root->left->right != NULL) {\n // right = recursion(root->left->right);\n // }\n // include += left + right;\n // }\n // if(root->right != NULL) {\n // int left = 0, right = 0;\n // if(root->right->left != NULL) {\n // left = recursion(root->right->left);\n // }\n // if(root->right->right != NULL) {\n // right = recursion(root->right->right);\n // }\n // include += left + right;\n // }\n\n // //exclude\n // int exclude = 0;\n // if(root->left != NULL) {\n // exclude += recursion(root->left);\n // }\n // if(root->right != NULL) {\n // exclude += recursion(root->right);\n // }\n // int sum = max(include, exclude);\n // return sum;\n // }\n\n int memoization(TreeNode* root, unordered_map<TreeNode*, int>& mp) {\n if(root == NULL) return 0;\n if(root->left == NULL && root->right == NULL) return root->val;\n if(mp.find(root) != mp.end()) return mp[root];\n\n //include\n int include = root->val;\n if(root->left != NULL) {\n int left = 0, right = 0;\n if(root->left->left != NULL) {\n left = memoization(root->left->left, mp);\n }\n if(root->left->right != NULL) {\n right = memoization(root->left->right, mp);\n }\n include += left + right;\n }\n if(root->right != NULL) {\n int left = 0, right = 0;\n if(root->right->left != NULL) {\n left = memoization(root->right->left, mp);\n }\n if(root->right->right != NULL) {\n right = memoization(root->right->right, mp);\n }\n include += left + right;\n }\n\n //exclude\n int exclude = 0;\n if(root->left != NULL) {\n exclude += memoization(root->left, mp);\n }\n if(root->right != NULL) {\n exclude += memoization(root->right, mp);\n }\n mp[root] = max(include, exclude);\n return mp[root];\n }\n\n int rob(TreeNode* root) {\n // return recursion(root);\n\n unordered_map<TreeNode*, int> mp;\n return memoization(root, mp);\n }\n};", "memory": "21020" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // int recursion(TreeNode* root) {\n // if(root == NULL) return 0;\n\n // if(root->left == NULL && root->right == NULL) return root->val;\n\n // //include\n // int include = root->val;\n // if(root->left != NULL) {\n // int left = 0, right = 0;\n // if(root->left->left != NULL) {\n // left = recursion(root->left->left);\n // }\n // if(root->left->right != NULL) {\n // right = recursion(root->left->right);\n // }\n // include += left + right;\n // }\n // if(root->right != NULL) {\n // int left = 0, right = 0;\n // if(root->right->left != NULL) {\n // left = recursion(root->right->left);\n // }\n // if(root->right->right != NULL) {\n // right = recursion(root->right->right);\n // }\n // include += left + right;\n // }\n\n // //exclude\n // int exclude = 0;\n // if(root->left != NULL) {\n // exclude += recursion(root->left);\n // }\n // if(root->right != NULL) {\n // exclude += recursion(root->right);\n // }\n // int sum = max(include, exclude);\n // return sum;\n // }\n\n int memoization(TreeNode* root, unordered_map<TreeNode*, int>& mp) {\n if(root == NULL) return 0;\n if(root->left == NULL && root->right == NULL) return root->val;\n if(mp.find(root) != mp.end()) return mp[root];\n\n //include\n int include = root->val;\n if(root->left != NULL) {\n include += memoization(root->left->left, mp) + memoization(root->left->right, mp);\n }\n if(root->right != NULL) {\n include += memoization(root->right->left, mp) + memoization(root->right->right, mp);\n }\n\n //exclude\n int exclude = memoization(root->left, mp) + memoization(root->right, mp);\n return mp[root] = max(include, exclude);\n }\n\n int rob(TreeNode* root) {\n // return recursion(root);\n\n unordered_map<TreeNode*, int> mp;\n return memoization(root, mp);\n }\n};", "memory": "21020" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n int level = 0, ret = 0;\n queue<TreeNode*> q;\n unordered_map<int, vector<TreeNode*>> m;\n unordered_map<TreeNode*, int> dp;\n q.push(root);\n while(!q.empty()){\n int n = q.size();\n for(int i=0; i<n; i++){\n TreeNode* tmp = q.front();\n q.pop();\n m[level].push_back(tmp);\n if(tmp->left){\n q.push(tmp->left);\n }\n if(tmp->right){\n q.push(tmp->right);\n }\n }\n level++;\n }\n for(int i=level-1; i>=0; i--){\n for(auto node:m[i]){\n int tmp1 = 0, tmp2 = node->val;\n if(node->left){\n tmp1 += dp[node->left];\n if(node->left->left){\n tmp2 += dp[node->left->left];\n }\n if(node->left->right){\n tmp2 += dp[node->left->right];\n }\n }\n if(node->right){\n tmp1 += dp[node->right];\n if(node->right->left){\n tmp2 += dp[node->right->left];\n }\n if(node->right->right){\n tmp2 += dp[node->right->right];\n }\n }\n dp[node] = max(tmp1, tmp2);\n }\n }\n return dp[root];\n }\n};", "memory": "21231" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<TreeNode *, int> mp;\n int helper(TreeNode *root) {\n if(!root)\n return 0;\n if(root-> left == NULL && root->right == NULL)\n return root->val;\n if(mp.find(root) != mp.end())\n return mp[root];\n \n int res1 = helper(root->left) + helper(root->right);\n int res2 = root->val;\n if(root->left)\n res2 += helper(root->left->left) + helper(root->left->right);\n if(root->right)\n res2 += helper(root->right->left) + helper(root->right->right);\n return mp[root] = max(res2,res1);\n }\n int rob(TreeNode* root) {\n return helper(root);\n }\n};", "memory": "21443" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*, int> mp;\n\n int helper(TreeNode* root){\n if(root == nullptr)return(0);\n if(root->left == nullptr && root->right == nullptr){mp[root]=root->val;return(root->val);}\n if(mp.find(root)!=mp.end())return(mp[root]);\n\n int ans = 0;\n int x,y;\n if(root->left != nullptr){x = helper(root->left->left) + helper(root->left->right);}\n else x = 0;\n if(root->right != nullptr){y = helper(root->right->left) + helper(root->right->right);}\n else y = 0;\n\n ans = x+y+root->val;\n ans = max(ans, helper(root->left)+helper(root->right));\n return(ans);\n } \n\n int helper1(TreeNode* root){\n int cur = 0;\n queue<TreeNode*> q;\n q.push(root);\n vector<pair<TreeNode*, int>> pq;\n\n while(q.size() > 0){\n int l = q.size();\n while(l--){\n auto r = q.front();\n q.pop();\n pq.push_back({r,cur});\n if(r->left != nullptr)q.push(r->left);\n if(r->right != nullptr)q.push(r->right);\n }\n cur++;\n }\n \n int l = pq.size();\n int x,y;\n mp[0] = 0;\n int ans = 0;\n for(int i=l-1;i>=0;i--){\n auto r = pq[i].first;\n if(r->left == nullptr && r->right == nullptr){mp[r] = r->val;continue;}\n if(r->left != nullptr){x = mp[r->left->left] + mp[r->left->right];}\n else x = 0;\n if(r->right != nullptr){y = mp[r->right->left] + mp[r->right->right];}\n else y = 0;\n ans = x+y+r->val;\n ans = max(ans, mp[r->left]+mp[r->right]);\n mp[r] = ans;\n }\n return(mp[root]);\n }\n\n int rob(TreeNode* root) {\n return(helper1(root));\n }\n};", "memory": "21654" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n map<TreeNode* ,int >mp ; \n\n int solve(TreeNode* root ){\n\n if (root == NULL ){\n return 0 ; \n }\n\n\n if( mp.find(root) != mp.end()){\n return mp[root] ; \n }\n \n int pick = root->val ;\n\n if (root->right){\n pick+=solve(root->right->right); \n pick+=solve(root->right->left); \n }\n\n if (root->left){\n pick+=solve(root->left->right);\n pick+=solve(root->left->left);\n\n }\n\n int notpick=0 ; \n notpick = solve(root->left) + solve(root->right); \n\n return mp[root] = max( pick , notpick ) ; \n\n }\n int rob(TreeNode* root) {\n return solve(root) ; \n }\n};", "memory": "21865" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
1
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<TreeNode*, int> mp;\n int rob(TreeNode* root) {\n int ans = 0;\n if(root == NULL) return 0;\n if(mp.find(root) != mp.end()) return mp[root];\n int a = root->val;\n if(root->left) a = a + rob(root->left->left) + rob(root->left->right);\n if(root->right) a = a + rob(root->right->left) + rob(root->right->right);\n\n int b = rob(root->left) + rob(root->right);\n\n return mp[root] = max(a,b);\n }\n};", "memory": "22076" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "auto speedUP = [](){\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n // int robbingHouse(TreeNode* root){\n // if(root == NULL){\n // return 0;\n // }\n\n // int robbingHome = 0, notrobbingHome = 0;\n\n // robbingHome += root->val;\n // if(root->left){\n // robbingHome += robbingHouse(root->left->left) + robbingHouse(root->left->right);\n // }\n // if(root->right){\n // robbingHome += robbingHouse(root->right->left) + robbingHouse(root->right->right);\n // }\n\n // notrobbingHome += robbingHouse(root->left) + robbingHouse(root->right);\n\n // return max(robbingHome, notrobbingHome);\n // }\n\n int robbingHouseUsingMemoization(TreeNode* root, unordered_map<TreeNode*, int> &dp){\n if(root == NULL){\n return 0;\n }\n\n if(dp.find(root) != dp.end()){\n return dp[root];\n }\n\n int robbingHome = 0, notrobbingHome = 0;\n\n robbingHome += root->val;\n if(root->left){\n robbingHome += robbingHouseUsingMemoization(root->left->left, dp) + robbingHouseUsingMemoization(root->left->right, dp);\n }\n if(root->right){\n robbingHome += robbingHouseUsingMemoization(root->right->left, dp) + robbingHouseUsingMemoization(root->right->right, dp);\n }\n\n notrobbingHome += robbingHouseUsingMemoization(root->left, dp) + robbingHouseUsingMemoization(root->right, dp);\n\n dp[root] = max(robbingHome, notrobbingHome);\n return dp[root];\n }\n int rob(TreeNode* root) {\n // return robbingHouse(root);\n\n unordered_map<TreeNode*, int> dp;\n return robbingHouseUsingMemoization(root, dp);\n }\n};", "memory": "22921" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<TreeNode*, int> memo;\n\n int rob(TreeNode* root) {\n if (!root) {\n return 0;\n } \n if (memo[root])\n return memo[root];\n\n // consider current root->val\n\n int max1 = 0;\n\n max1 += rob(root->left);\n max1 += rob(root->right);\n\n int max2 = root->val;\n if (root->left) {\n max2 += rob(root->left->left);\n max2 += rob(root->left->right);\n } \n if (root->right) {\n max2 += rob(root->right->left);\n max2 += rob(root->right->right);\n }\n int result = max(max1, max2);\n memo[root] = result;\n return result;\n }\n};", "memory": "23133" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int check(TreeNode* root,bool flag,unordered_map <TreeNode*,pair<int,int>> &ourmap){\n if(root == NULL){\n return 0;\n }\n int a = 0;\n if(flag){\n a = 1;\n } \n if(ourmap.count(root) > 0){\n if(a == 0){\n if(ourmap[root].first != -1){\n return ourmap[root].first;\n }\n }\n if(a == 1){\n if(ourmap[root].second != -1){\n return ourmap[root].second;\n } \n }\n }\n\n int res1 = 0;\n int res2 = 0;\n\n if(flag){\n res1 = check(root->left,flag,ourmap) + check(root->right,flag,ourmap);\n res2 = root->val + check(root->left,false,ourmap) + check(root->right,false,ourmap); \n }\n else{\n res1 = check(root->left,true,ourmap) + check(root->right,true,ourmap);\n }\n int res = max(res1,res2); \n if(ourmap.count(root) > 0){\n pair<int,int> p = ourmap[root];\n if(a == 0){\n p.first = res;\n }\n else{\n p.second = res;\n }\n } \n else{\n pair<int,int> p;\n if(a == 0){\n p.first = res;\n p.second = -1;\n }\n else{\n p.first = -1; \n p.second = res;\n }\n pair <TreeNode*,pair<int,int>> p1(root,p); \n ourmap.insert(p1); \n } \n\n return res; \n } \n\npublic:\n int rob(TreeNode* root) { \n unordered_map <TreeNode*,pair<int,int>> ourmap;\n int result = check(root,true,ourmap); \n\n return result; \n }\n};\n\n", "memory": "23344" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nmap<TreeNode*,pair<int,int>> cache;\nint proc(TreeNode* root,int p)\n{\n if(p == 0 && cache[root].first!=0) return cache[root].first;\n if(p == 1 && cache[root].second!=0) return cache[root].second; \n if(!root) return 0;\n int retval = 0;\n if(p == 0)\n {\n int r1 = 0 ,r2 = 0;\n r1 = proc(root->left,1);\n r2 = proc(root->right,1);\n \n retval = r1+r2+root->val;\n\n r1 = proc(root->left,0);\n r2 = proc(root->right,0);\n retval = max(retval,r1+r2);\n }\n else\n {\n int r1 = 0,r2 = 0;\n r1 = proc(root->left,0);\n r2 = proc(root->right,0);\n retval = r1+r2;\n }\n if(p == 0) cache[root].first = retval;\n if(p == 1) cache[root].second = retval;\n return retval;\n}\n\nint rob(TreeNode* root) \n{\n return proc(root,0);\n}\n};", "memory": "23555" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int f(TreeNode* root,unordered_map<TreeNode*,int>& dp){\n if(!root) return 0;\n if(dp.count(root)) return dp[root];\n int not_pick = f(root->left,dp) + f(root->right,dp);\n int pick = root->val;\n if(root->left){\n pick += f(root->left->left,dp) + f(root->left->right,dp);\n }\n if(root->right){\n pick += f(root->right->left,dp) + f(root->right->right,dp);\n }\n return dp[root] = max(pick,not_pick);\n }\npublic:\n int rob(TreeNode* root) {\n unordered_map<TreeNode*,int> dp;\n return f(root,dp);\n }\n};", "memory": "23766" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int f(TreeNode* root,unordered_map<TreeNode*,int>& dp){\n if(!root) return 0;\n if(dp.count(root)) return dp[root];\n int not_pick = f(root->left,dp) + f(root->right,dp);\n int pick = root->val;\n if(root->left){\n pick += f(root->left->left,dp) + f(root->left->right,dp);\n }\n if(root->right){\n pick += f(root->right->left,dp) + f(root->right->right,dp);\n }\n return dp[root] = max(pick,not_pick);\n }\npublic:\n int rob(TreeNode* root) {\n unordered_map<TreeNode*,int> dp;\n return f(root,dp);\n }\n};", "memory": "23766" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nint find(TreeNode*root,unordered_map<TreeNode*,int>&mp){\n if(root == nullptr) return 0;\n if(mp.find(root)!=mp.end()) return mp[root];\n int take = 1e9, nottake = 1e9;\n nottake = find(root->left,mp) + find(root->right,mp);\n\n take = root->val;\n if(root->left)\n take += find(root->left->left,mp) + find(root->left->right,mp);\n\n if(root->right)\n take += find(root->right->left,mp) + find(root->right->right,mp);\n\n return mp[root] = max(take,nottake);\n}\n int rob(TreeNode* root) {\n unordered_map<TreeNode*,int> mp;\n return find(root,mp);\n }\n};", "memory": "23978" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*,int>m;\n int n=0;\n void inOrder(TreeNode* root){\n if(root==NULL){\n return;\n }\n m[root]=n;\n n++;\n inOrder(root->left);\n inOrder(root->right);\n }\n int rec(TreeNode* root,vector<int>&dp){\n if(root==NULL){\n return 0;\n }\n if(dp[m[root]]!=-1){\n return dp[m[root]];\n }\n int ans=root->val;\n if(root->left){\n ans+=(rec(root->left->left,dp)+rec(root->left->right,dp));\n }\n if(root->right){\n ans+=(rec(root->right->left,dp)+rec(root->right->right,dp));\n }\n return dp[m[root]]=max(ans,rec(root->left,dp)+rec(root->right,dp));\n }\n int rob(TreeNode* root) {\n inOrder(root);\n vector<int>dp(n+1,-1);\n return rec(root,dp);\n }\n};", "memory": "24189" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*,pair<int,int>> mp;\n int rob(TreeNode* root) {\n ios::sync_with_stdio(false);\n ios_base::sync_with_stdio(false);\n ios::sync_with_stdio(false);\n cout.tie(nullptr);\n cin.tie(nullptr);\n if(root == NULL)\n return 0;\n if(mp.find(root) != mp.end())\n return max(mp[root].first,mp[root].second);\n int not_take = rob(root->left) + rob(root->right);\n int take = mp[root->left].first + mp[root->right].first + root->val;\n mp[root].first = not_take;\n mp[root].second = take;\n return max(take,not_take);\n }\n};", "memory": "24400" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int rob(TreeNode* root) {\n std::unordered_map<TreeNode*, int> memo;\n \n std::function<int(TreeNode*)> dfs = [&](TreeNode *curr) -> int {\n if (!curr) {\n return 0;\n }\n if (memo.count(curr)) {\n return memo[curr];\n }\n \n int robCurr = curr->val;\n if (curr->left) {\n robCurr += dfs(curr->left->left) + dfs(curr->left->right);\n }\n if (curr->right) {\n robCurr += dfs(curr->right->left) + dfs(curr->right->right);\n }\n \n int skipCurr = dfs(curr->left) + dfs(curr->right);\n memo[curr] = std::max(robCurr, skipCurr);\n return memo[curr];\n };\n\n return dfs(root);\n }\n};\n", "memory": "24611" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int rob(TreeNode* root) {\n std::unordered_map<TreeNode*, int> memo;\n \n std::function<int(TreeNode*)> dfs = [&](TreeNode *curr) -> int {\n if (!curr) {\n return 0;\n }\n if (memo.count(curr)) {\n return memo[curr];\n }\n \n int robCurr = curr->val;\n if (curr->left) {\n robCurr += dfs(curr->left->left) + dfs(curr->left->right);\n }\n if (curr->right) {\n robCurr += dfs(curr->right->left) + dfs(curr->right->right);\n }\n \n int skipCurr = dfs(curr->left) + dfs(curr->right);\n return memo[curr] = std::max(robCurr, skipCurr);\n };\n\n return dfs(root);\n }\n};\n", "memory": "24611" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int* robSub(TreeNode* pNode) \n {\n if (pNode == nullptr) {\n return new int[2]{0,0};\n }\n int* pLeft = robSub(pNode->left);\n int* pRight = robSub(pNode->right);\n int* apRes = new int[2];\n // Root is not robbed\n apRes[0] = std::max(pLeft[0],pLeft[1]) + std::max(pRight[0], pRight[1]);\n // Root is robbed and so we get the two subnodes NOT ROBBED!\n apRes[1] = pNode->val + pLeft[0] + pRight[0];\n return apRes;\n }\n\n\n int rob(TreeNode* root) \n {\n int* res = robSub(root);\n // res[] == rootNotRobbed; res[1] == rootIsRobbed;\n return std::max(res[0], res[1]);\n }\n};", "memory": "24823" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n int*options=travel(root);\n return max(options[0],options[1]);\n }\n int* travel(TreeNode* root){\n if(!root)\n return new int[2]{0,0};\n int*leftch=travel(root->left);\n int*rightch=travel(root->right);\n int*options=new int[2];\n options[0]=root->val+leftch[1]+rightch[1];\n options[1]=max(leftch[0],leftch[1])+max(rightch[0],rightch[1]);\n return options;\n }\n};", "memory": "25034" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n unordered_map<TreeNode*, vector<int>> memo;\n return dp(root, 0, memo);\n }\n\n int dp(TreeNode* node, int didRobPrevHouse, unordered_map<TreeNode*, vector<int>>& memo) {\n if (!node) {\n return 0;\n }\n if (node->left == nullptr && node->right == nullptr) {\n return didRobPrevHouse ? 0 : node->val; \n }\n auto iter = memo.find(node);\n if (iter != memo.end() && iter->second[didRobPrevHouse] != -1) {\n return iter->second[didRobPrevHouse];\n }\n if (memo[node].size() == 0) {\n memo[node] = {-1, -1};\n }\n int money = -1;\n if (didRobPrevHouse) {\n money = dp(node->left, 0, memo) + dp(node->right, 0, memo);\n } else {\n int moneyIfWeDontRob = dp(node->left, 0, memo) + dp(node->right, 0, memo);\n int moneyIfWeRob = node->val + dp(node->left, 1, memo) + dp(node->right, 1, memo);\n money = max(moneyIfWeDontRob, moneyIfWeRob);\n }\n memo[node][didRobPrevHouse] = money;\n return money;\n }\n};", "memory": "25245" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<TreeNode*, int> arr;\n int helper(TreeNode* root, int canTake, vector<vector<int>> &dp){\n if(root == NULL) return 0;\n if(dp[arr[root]][canTake] != -1) return dp[arr[root]][canTake];\n if(canTake == 1){\n //take\n int take = root->val + helper(root->left, !canTake, dp) + helper(root->right, !canTake, dp);\n // nottake\n int notTake = helper(root->left, canTake, dp) + helper(root->right, canTake, dp);\n return dp[arr[root]][canTake] = max(take, notTake);\n }\n else{\n return dp[arr[root]][canTake] = helper(root->left, !canTake, dp) + helper(root->right, !canTake, dp);\n }\n }\n\n void mark(TreeNode* root, int& idx){\n if(root == NULL) return;\n arr[root] = idx++;\n mark(root->left, idx);\n mark(root->right, idx);\n }\n\n int rob(TreeNode* root) {\n int idx = 0;\n mark(root, idx);\n vector<vector<int>> dp(idx+1, vector<int>(2, -1));\n return helper(root, 1, dp);\n }\n};", "memory": "25456" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n if(root==NULL)\n return 0;\n vector<TreeNode*>arr;\n unordered_map<TreeNode*, int>mp;\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty())\n {\n int k = q.size();\n for(int i=0; i<k; i++)\n {\n TreeNode*temp = q.front();\n q.pop();\n if(temp)\n {\n mp[temp] = arr.size();\n arr.push_back(temp);\n }\n else\n {\n arr.push_back(NULL);\n continue;\n }\n \n q.push(temp->left);\n q.push(temp->right);\n }\n }\n // for(int i=0; i<arr.size(); i++)\n // cout<<arr[i]->val<<\" \";\n \n // n, 2*n+1, 2*n+2, 0/1 max(dp[0][0], dp[0][1])\n // dp[i][1] = i->val + max(dp[2*i+1][0], dp[2*i+2][1]);\n // dp[i][0] = max(dp[2*i+1][0], dp[2*i+1][1]) + max(dp[2*])\n int n = arr.size();\n vector<vector<int>>dp(n, vector<int>(2));\n for(int i=n-1; i>=0; i--)\n {\n if(arr[i]==NULL)\n continue;\n else\n {\n dp[i][1] = arr[i]->val;\n TreeNode*node = arr[i];\n if(node)\n {\n if(node->left)\n dp[i][1]+=dp[mp[node->left]][0];\n if(node->right)\n dp[i][1]+=dp[mp[node->right]][0];\n if(node->left)\n dp[i][0] = max(dp[mp[node->left]][0], dp[mp[node->left]][1]);\n if(node->right)\n dp[i][0]+=max(dp[mp[node->right]][0], dp[mp[node->right]][1]);\n }\n }\n cout<<endl;\n cout<<i<<\": \"<<dp[i][0]<<\" \"<<dp[i][1]<<endl;\n }\n return max(dp[0][0], dp[0][1]);\n }\n};", "memory": "25668" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n// class Solution {\n// public:\n// int dfs(bool flag,TreeNode* node){\n// if(!node)return 0;\n// int pick=0;\n// if(flag){\n \n// pick = node->val + dfs(false, node->left) + dfs(false, node->right);\n// }\n// int not_pick = dfs(true, node->left) + dfs(true, node->right);\n// return max(pick,not_pick);\n// }\n// int rob(TreeNode* root) {\n// bool flag=true;\n// return dfs(flag,root);\n// }\n// };\n\n\nclass Solution {\npublic:\n unordered_map<TreeNode*, int> node_to_index; // Map to store index of each node\n vector<vector<int>> memo; // 2D vector to store results, memo[node_index][flag]\n \n // Function to assign a unique index to each node\n int assignIndices(TreeNode* root, int idx) {\n if (!root) return idx;\n node_to_index[root] = idx; // Assign unique index to this node\n idx = assignIndices(root->left, idx + 1);\n return assignIndices(root->right, idx);\n }\n\n int dfs(bool flag, TreeNode* node){\n if (!node) return 0;\n \n // Get the index of the current node\n int node_idx = node_to_index[node];\n\n // Memoization: check if the result is already computed\n if (memo[node_idx][flag] != -1) return memo[node_idx][flag];\n \n int pick = 0;\n // If we are allowed to pick this node\n if (flag) {\n pick = node->val + dfs(false, node->left) + dfs(false, node->right);\n }\n\n // We can either pick or not pick this node\n int not_pick = dfs(true, node->left) + dfs(true, node->right);\n\n // Store the result in memo before returning\n return memo[node_idx][flag] = max(pick, not_pick);\n }\n\n int rob(TreeNode* root) {\n if (!root) return 0;\n \n // Assign unique indices to each node starting from 0\n int total_nodes = assignIndices(root, 0);\n\n // Initialize memo vector with size total_nodes x 2 (for flag = true/false)\n memo = vector<vector<int>>(total_nodes, vector<int>(2, -1));\n\n return dfs(true, root);\n }\n};\n", "memory": "25879" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int x=0;\n unordered_map<TreeNode*,int>mp;\n void tvs(TreeNode* root){\n if(root==NULL)return;\n tvs(root->left);\n mp[root]=x++;\n tvs(root->right);\n }\n int f(TreeNode* root , int j, vector<vector<int>>&dp){\n if(root==NULL)return 0;\n int i=mp[root];\n if(dp[i][j]!=-1)return dp[i][j];\n if(j==0){\n int lefti=max(f(root->left,0,dp),f(root->left,1,dp));\n int righti=max(f(root->right,0,dp),f(root->right,1,dp));\n dp[i][j]=lefti+righti;\n }else{\n int lefti=f(root->left,0,dp);\n int righti=f(root->right,0,dp);\n dp[i][j]=(root->val)+lefti+righti;\n }\n return dp[i][j]; \n }\n int rob(TreeNode* root) {\n tvs(root);\n vector<vector<int>>dp(x,vector<int>(2,-1));\n int ans1=f(root,0,dp);\n int ans2=f(root,1,dp);\n return max(ans1,ans2);\n }\n};", "memory": "26090" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int fun(TreeNode* root,int x,map<pair<TreeNode*,int>,int>& dp){\n if(root==nullptr)\n return 0;\n if(root->left==nullptr && root->right==nullptr){\n if(x==0){\n return 0;\n }\n else\n return root->val;\n }\n if(dp.find({root,x})!=dp.end())\n return dp[{root,x}];\n int ans=0;\n if(x==0){\n ans=max(fun(root->left,0,dp),fun(root->left,1,dp))+max(fun(root->right,0,dp),fun(root->right,1,dp));\n }\n else{\n ans=root->val+fun(root->left,0,dp)+fun(root->right,0,dp);\n }\n return dp[{root,x}]=ans;\n }\n int rob(TreeNode* root) {\n if(root==nullptr)\n return 0;\n map<pair<TreeNode*,int>,int> dp;\n int withrobbery = root->val + fun(root->left,0,dp)+fun(root->right,0,dp);\n int withoutrobbery = max(fun(root->left,0,dp),fun(root->left,1,dp))+max(fun(root->right,0,dp),fun(root->right,1,dp));\n return max(withrobbery,withoutrobbery);\n }\n};", "memory": "26301" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int fun(TreeNode* root,int x,map<pair<TreeNode*,int>,int> &mp){\n if(root==NULL)\n return 0;\n if(root->left==NULL && root->right==NULL){\n if(x==0)\n return 0;\n else\n return root->val;\n }\n if(mp.find({root,x})!=mp.end())\n return mp[{root,x}];\n int ans=0;\n if(x==0){\n ans=max(fun(root->left,0,mp),fun(root->left,1,mp))+max(fun(root->right,0,mp),fun(root->right,1,mp));\n }\n else{\n ans=root->val+fun(root->left,0,mp)+fun(root->right,0,mp);\n }\n return mp[{root,x}]=ans;\n }\n int rob(TreeNode* root) {\n if(root==NULL)\n return 0;\n map<pair<TreeNode*,int>,int> mp;\n int withroot=root->val+fun(root->left,0,mp)+fun(root->right,0,mp);\n int withoutroot=max(fun(root->left,0,mp),fun(root->left,1,mp))+max(fun(root->right,0,mp),fun(root->right,1,mp));\n return max(withroot,withoutroot);\n }\n};", "memory": "26513" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nunordered_map<TreeNode*,TreeNode*>m;\nunordered_map<TreeNode*,int>dp;\nvoid dfs(TreeNode* root)\n{\nif(root==NULL)\n{\nreturn;\n}\nint d=root->val+dp[m[m[root]]];\ndfs(root->left);\ndfs(root->right);\nint c=dp[root->left]+dp[root->right]+dp[m[root]];\nif(root->left)\n{\nd+=dp[root->left->left];\nd+=dp[root->left->right];\n}\nif(root->right)\n{\nd+=dp[root->right->left];\nd+=dp[root->right->right];\n}\ndp[root]=max(c,d);\n}\n\n\n int rob(TreeNode* root) {\n queue<TreeNode*>q;\n q.push(root);\n while(!q.empty())\n {\n TreeNode* x=q.front();\n q.pop();\n if(x->left)\n {\n m[x->left]=x;\n q.push(x->left);\n }\n if(x->right)\n {\n q.push(x->right);\n m[x->right]=x;\n }\n }\n dfs(root);\n return dp[root];\n }\n};", "memory": "26724" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<pair<TreeNode*, int>, int> mp;\n int fun(TreeNode* root, int flag) {\n if (root == nullptr) return 0;\n if (root -> left == nullptr && root -> right == nullptr) {\n if (flag == 1) return root -> val;\n return 0;\n }\n if (mp.find({root, flag}) != mp.end()) {\n return mp[{root, flag}];\n }\n int a = 0;\n int b = 0;\n int c = 0;\n if (flag == 0) {\n a = fun(root -> left, 1) + fun(root -> right, 1);\n } else {\n b = root -> val + fun(root -> left, 0) + fun(root -> right, 0);\n c = fun(root -> left, 1) + fun(root -> right, 1);\n }\n int ans = max(a, max(b, c));\n return mp[{root, flag}] = ans;\n }\n int rob(TreeNode* root) {\n mp.clear();\n return max(fun(root, 0), fun(root, 1));\n }\n};", "memory": "26935" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "\nclass Solution\n{\n\tpublic:\n\t\tint rob(TreeNode* root)\n\t\t{\n\t\t\tint ans = 0;\n\t\t\tunordered_map<TreeNode*, int> dp_rob, dp_pass;\n\t\t\t\n\t\t\tdp_rob[root] = payDay(root, dp_rob, dp_pass);\n\t\t\tdp_pass[root] = payDay(root->left, dp_rob, dp_pass), payDay(root->right, dp_rob, dp_pass);\n\n\t\t\treturn max(dp_rob[root], dp_pass[root]);\n\t\t}\n\n\t\tint payDay(TreeNode* root, unordered_map<TreeNode*, int>& dp_rob, unordered_map<TreeNode*, int>& dp_pass)\n\t\t{\n\t\t\tif(root == nullptr) return 0;\n\n\t\t\tTreeNode* l = root->left;\n\t\t\tTreeNode* r = root->right;\n\t\t\tTreeNode *ll = nullptr, *lr = nullptr;\n\t\t\tTreeNode *rl = nullptr, *rr = nullptr;\n\t\t\tif(l != nullptr)\n\t\t\t{\n\t\t\t\tll = l->left;\n\t\t\t\tlr = l->right;\n\t\t\t}\n\t\t\tif(r != nullptr)\n\t\t\t{\n\t\t\t\trl = r->left;\n\t\t\t\trr = r->right;\n\t\t\t}\n\n\t\t\tif(dp_rob.find(root) == dp_rob.end())\n\t\t\t\tdp_rob[root] = root->val + payDay(ll, dp_rob, dp_pass) + payDay(lr, dp_rob, dp_pass) + \n\t\t\t\t\tpayDay(rl, dp_rob, dp_pass) + payDay(rr, dp_rob, dp_pass);\n\t\t\tif(dp_pass.find(root) == dp_pass.end())\n\t\t\t\tdp_pass[root] = payDay(l, dp_rob, dp_pass) + payDay(r, dp_rob, dp_pass);\n\n\t\t\treturn max(dp_rob[root], dp_pass[root]);\n\t\t}\n};\n\n", "memory": "26935" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "\nclass Solution\n{\n\tpublic:\n\t\tint rob(TreeNode* root)\n\t\t{\n\t\t\tint ans = 0;\n\t\t\tunordered_map<TreeNode*, int> dp_rob, dp_pass;\n\t\t\t\n\t\t\tdp_rob[root] = payDay(root, dp_rob, dp_pass);\n\t\t\tdp_pass[root] = payDay(root->left, dp_rob, dp_pass), payDay(root->right, dp_rob, dp_pass);\n\n\t\t\treturn max(dp_rob[root], dp_pass[root]);\n\t\t}\n\n\t\tint payDay(TreeNode* root, unordered_map<TreeNode*, int>& dp_rob, unordered_map<TreeNode*, int>& dp_pass)\n\t\t{\n\t\t\tif(root == nullptr) return 0;\n\n\t\t\tTreeNode* l = root->left;\n\t\t\tTreeNode* r = root->right;\n\t\t\tTreeNode *ll = nullptr, *lr = nullptr;\n\t\t\tTreeNode *rl = nullptr, *rr = nullptr;\n\t\t\tif(l != nullptr)\n\t\t\t{\n\t\t\t\tll = l->left;\n\t\t\t\tlr = l->right;\n\t\t\t}\n\t\t\tif(r != nullptr)\n\t\t\t{\n\t\t\t\trl = r->left;\n\t\t\t\trr = r->right;\n\t\t\t}\n\n\t\t\tif(dp_rob.find(root) == dp_rob.end())\n\t\t\t\tdp_rob[root] = root->val + payDay(ll, dp_rob, dp_pass) + payDay(lr, dp_rob, dp_pass) + \n\t\t\t\t\tpayDay(rl, dp_rob, dp_pass) + payDay(rr, dp_rob, dp_pass);\n\t\t\tif(dp_pass.find(root) == dp_pass.end())\n\t\t\t\tdp_pass[root] = payDay(l, dp_rob, dp_pass) + payDay(r, dp_rob, dp_pass);\n\n\t\t\treturn max(dp_rob[root], dp_pass[root]);\n\t\t}\n};\n\n", "memory": "27146" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "\nclass Solution\n{\n\tpublic:\n\t\tint rob(TreeNode* root)\n\t\t{\n\t\t\tint ans = 0;\n\t\t\tunordered_map<TreeNode*, int> dp_rob, dp_pass;\n\t\t\t\n\t\t\tdp_rob[root] = payDay(root, dp_rob, dp_pass);\n\t\t\tdp_pass[root] = payDay(root->left, dp_rob, dp_pass), payDay(root->right, dp_rob, dp_pass);\n\n\t\t\treturn max(dp_rob[root], dp_pass[root]);\n\t\t}\n\n\t\tint payDay(TreeNode* root, unordered_map<TreeNode*, int>& dp_rob, unordered_map<TreeNode*, int>& dp_pass)\n\t\t{\n\t\t\tif(root == nullptr) return 0;\n\n\t\t\tTreeNode* l = root->left;\n\t\t\tTreeNode* r = root->right;\n\t\t\tTreeNode *ll = nullptr, *lr = nullptr;\n\t\t\tTreeNode *rl = nullptr, *rr = nullptr;\n\t\t\tif(l != nullptr)\n\t\t\t{\n\t\t\t\tll = l->left;\n\t\t\t\tlr = l->right;\n\t\t\t}\n\t\t\tif(r != nullptr)\n\t\t\t{\n\t\t\t\trl = r->left;\n\t\t\t\trr = r->right;\n\t\t\t}\n\n\t\t\tif(dp_rob.find(root) == dp_rob.end())\n\t\t\t\tdp_rob[root] = root->val + payDay(ll, dp_rob, dp_pass) + payDay(lr, dp_rob, dp_pass) + \n\t\t\t\t\tpayDay(rl, dp_rob, dp_pass) + payDay(rr, dp_rob, dp_pass);\n\t\t\tif(dp_pass.find(root) == dp_pass.end())\n\t\t\t\tdp_pass[root] = payDay(l, dp_rob, dp_pass) + payDay(r, dp_rob, dp_pass);\n\n\t\t\treturn max(dp_rob[root], dp_pass[root]);\n\t\t}\n};\n\n", "memory": "27146" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int getMaxValue(TreeNode* root, int canRob, unordered_map<TreeNode*, vector<int>>& cache) {\n if (!root)\n return 0;\n \n if (cache.contains(root)) {\n return cache[root][canRob];\n }\n\n int skip = getMaxValue(root->left, 1, cache) + getMaxValue(root->right, 1, cache);\n int pick = (\n root->val \n + getMaxValue(root->left, 0, cache) \n + getMaxValue(root->right, 0, cache)\n );\n\n cache[root] = {skip, pick};\n\n return max(canRob ? pick : 0, skip);\n }\npublic:\n int rob(TreeNode* root) {\n unordered_map<TreeNode*, vector<int>> cache;\n return getMaxValue(root, 1, cache);\n }\n};", "memory": "27358" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n int getMaxValue(TreeNode* root, int canRob, unordered_map<TreeNode*, vector<int>>& cache) {\n if (!root)\n return 0;\n \n if (cache.contains(root) /* && cache[root][canRob] != -1 */) {\n return cache[root][canRob];\n }\n\n // if (!cache.contains(root)) \n // cache[root] = {-1, -1};\n\n int skip = getMaxValue(root->left, 1, cache) + getMaxValue(root->right, 1, cache);\n int pick = (\n root->val \n + getMaxValue(root->left, 0, cache) \n + getMaxValue(root->right, 0, cache)\n );\n\n cache[root] = {skip, pick};\n\n return max(canRob ? pick : 0, skip);\n }\npublic:\n int rob(TreeNode* root) {\n unordered_map<TreeNode*, vector<int>> cache;\n return getMaxValue(root, 1, cache);\n }\n};", "memory": "27569" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*,vector<int>>mp;\n int dp(TreeNode* root, bool canRob){\n if(root == NULL) return 0;\n \n if(mp.find(root) != mp.end() && mp[root][canRob] != -1){\n return mp[root][canRob];\n }\n mp[root] = {-1,-1};\n int rob = canRob ? root->val+dp(root->left,false)+dp(root->right,false) : 0;\n\n int noRob = dp(root->left,true)+dp(root->right,true);\n\n return mp[root][canRob] = max(rob,noRob);\n }\n\n \n int rob(TreeNode* root) {\n return dp(root,true);\n }\n};", "memory": "27780" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> solve(TreeNode* root) {\n if (root == nullptr) return vector<int>(2, 0);\n if (root->left == nullptr && root->right == nullptr) return {root->val, 0};\n auto left = solve(root->left);\n auto right = solve(root->right);\n vector<int> ans(2);\n ans[0] = root->val + left[1] + right[1];\n ans[1] = max(left[0], left[1]) + max(right[0], right[1]);\n return ans;\n }\n int rob(TreeNode* root) {\n auto ans = solve(root);\n return max(ans[0], ans[1]);\n }\n};", "memory": "27780" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*,vector<int>>mp;\n int dp(TreeNode* root, bool canRob){\n if(root == NULL) return 0;\n \n if(mp.find(root) != mp.end() && mp[root][canRob] != -1){\n return mp[root][canRob];\n }\n mp[root]={-1,-1};\n int rob = canRob ? root->val+dp(root->left,false)+dp(root->right,false) : 0;\n\n int noRob = dp(root->left,true)+dp(root->right,true);\n\n return mp[root][canRob] = max(rob,noRob);\n }\n\n \n int rob(TreeNode* root) {\n return dp(root,true);\n }\n};", "memory": "27991" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n\nclass Solution {\n vector<unordered_map<TreeNode*,int>>dp;\npublic: \n int solve(TreeNode* root, bool can_do){\n if(!root)return 0;\n if(dp[can_do].count(root))return dp[can_do][root];\n int chori = 0;\n if(can_do)chori = root->val + solve(root->left, !can_do)+solve(root->right,!can_do);\n int nochori = solve(root->left, true)+solve(root->right,true);\n return dp[can_do][root]=max(chori, nochori);\n }\n int rob(TreeNode* root) {\n dp.resize(2);\n return solve(root,true);\n }\n};", "memory": "28203" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*,int> dp[2];\n int fn(TreeNode* root , bool prev){\n if(!root) return 0;\n if(dp[prev].count(root)) return dp[prev][root];\n int ans = 0;\n if(!prev) ans = root->val + fn(root->left , 1)+ fn(root->right , 1);\n ans = max(ans , fn(root->left,0) + fn(root->right,0));\n return dp[prev][root] = ans;\n }\n int rob(TreeNode* root) {\n return fn(root , 0);\n }\n};", "memory": "28203" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n unordered_map<TreeNode*, int> flag0;\n unordered_map<TreeNode*, int> flag1; \n int rob(TreeNode* root) {\n return dfs(root, 1);\n }\n\n int dfs(TreeNode* node, int flag)\n {\n if(!node)\n return 0;\n\n if(flag == 0)\n {\n if(flag0.find(node) != flag0.end())\n return flag0[node];\n\n flag0[node] = dfs(node->left, 1) + dfs(node->right, 1);\n return flag0[node];\n }\n else\n {\n if(flag1.find(node) != flag1.end())\n return flag1[node];\n\n int x = node->val + dfs(node->left, 0) + dfs(node->right, 0);\n int y = dfs(node->left, 1) + dfs(node->right, 1);\n\n flag1[node] = max(x, y);\n return flag1[node];\n }\n }\n};", "memory": "28414" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
2
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n void helper(TreeNode *curr,unordered_map<TreeNode *,int>&dp1,unordered_map<TreeNode *,int>&dp2){\n int sum1=curr->val; //when we take curr node \n int sum2=0; //when we didnot take curr node \n\n\n if(curr->left!=NULL){\n helper(curr->left,dp1,dp2);\n sum1+=dp2[curr->left];\n sum2+=max(dp2[curr->left],dp1[curr->left]);\n }\n if(curr->right!=NULL){\n helper(curr->right,dp1,dp2);\n sum1+=dp2[curr->right];\n sum2+=max(dp2[curr->right],dp1[curr->right]);\n }\n\n dp1[curr]=sum1;\n dp2[curr]=sum2;\n\n return;\n\n\n\n }\n int rob(TreeNode* root) {\n unordered_map<TreeNode *, int>dp1; //for a subtree whose root is x its represent the maximum amount the robber can rob if he steal money from x node in thatn subtree\n unordered_map<TreeNode *, int>dp2; //for a subtree whose root is x its represent the maximum amount the robber can rob if he didnot steal money from x node in thatn subtree\n\n \n\n helper(root,dp1,dp2);\n\n return max(dp1[root],dp2[root]);\n \n }\n};", "memory": "28414" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<pair<TreeNode*, int>, int> mp;\n int util(TreeNode* root, int parent)\n {\n if(!root) return 0;\n\n int take = 0;\n int notTake = 0;\n if(mp.find({root,parent}) != mp.end()) return mp[{root,parent}];\n if(parent == 0)\n {\n take += root->val + util(root->left, 1) + util(root->right, 1);\n notTake = util(root->left, 0) + util(root->right, 0);\n }\n else\n {\n notTake = util(root->left, 0) + util(root->right, 0);\n }\n\n return mp[{root,parent}] = max(take, notTake);\n\n }\n int rob(TreeNode* root) {\n\n return util(root, 0);\n }\n};", "memory": "29259" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int solve(TreeNode *root,bool prev,map<pair<TreeNode*,bool>,int> &dp){\n if(!root) return 0;\n if(dp.find({root,prev})!=dp.end()) return dp[{root,prev}];\n int a=0,b=0;\n if(!prev){\n a=root->val+solve(root->left,true,dp)+solve(root->right,true,dp);\n }\n b=solve(root->left,0,dp)+solve(root->right,0,dp);\n return dp[{root,prev}]=max(a,b);\n }\n int rob(TreeNode* root) {\n //so if prev=1 previous one is taken and i cannot take present node\n //if prev==0 previous is not picked so i can either take or not take the present node\n map<pair<TreeNode*,bool>,int> mp;\n return max(solve(root,false,mp),solve(root,true,mp));\n }\n};", "memory": "29259" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int util(TreeNode *root, bool canTake, map<pair<TreeNode*,bool>, int> &m){\n if(!root) return 0;\n\n auto key = make_pair(root, canTake);\n if(m.find(key)!=m.end()) return m[key];\n if(canTake){\n int take = root->val+util(root->left, false, m) + util(root->right, false, m);\n int leave = util(root->left, true, m) + util(root->right, true, m);\n m[key]= max(take, leave);\n }else{\n int leave = util(root->left, true, m) + util(root->right, true, m);\n m[key]= leave;\n }\n return m[key];\n }\n int rob(TreeNode* root) {\n map<pair<TreeNode*,bool>, int> m;\n int ans = util(root, true, m);\n return ans;\n }\n};", "memory": "29470" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int solve(TreeNode* root,int prev,map<pair<TreeNode*,int>,int>&dp){\n if(!root){\n return 0;\n }\n if(dp.find({root,prev})!=dp.end()){\n return dp[{root,prev}];\n }\n int take=0;\n if(!prev){\n take=root->val+solve(root->left,1,dp)+solve(root->right,1,dp);\n }\n int nottake=solve(root->left,0,dp)+solve(root->right,0,dp);\n return dp[{root,prev}]=max(take,nottake);\n }\n int rob(TreeNode* root) {\n map<pair<TreeNode*,int>,int> dp;\n return solve(root,0,dp);\n }\n};", "memory": "29470" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int solve( TreeNode * root , bool can)\n {\n if( root == NULL )\n {\n return 0;\n }\n int ans = 0;\n if( can )\n {\n // include \n int in = root->val;\n in += solve( root->left , false);\n in += solve( root->right , false );\n\n // exclude \n int ex = 0;\n ex += solve( root->left , true );\n ex += solve( root->right , true );\n\n ans = max( in , ex );\n\n }\n else\n {\n ans += solve( root->left , true );\n ans += solve( root->right , true );\n }\n return ans;\n }\n struct pair_hash {\n template <class T1, class T2>\n std::size_t operator()(const std::pair<T1, T2>& p) const {\n auto hash1 = std::hash<T1>{}(p.first);\n auto hash2 = std::hash<T2>{}(p.second);\n return hash1 ^ hash2;\n }\n};\n // non continuous DP\n int solveUsingMem( TreeNode * root , bool can , unordered_map<std::pair<TreeNode*, bool>, int, pair_hash> &dp )\n {\n if( root == NULL )\n {\n return 0;\n }\n if( dp.find({root , can }) != dp.end())\n {\n return dp[{ root , can }];\n }\n int ans = 0;\n if( can )\n {\n // include \n int in = root->val;\n in += solveUsingMem( root->left , false, dp);\n in += solveUsingMem( root->right , false , dp);\n\n // exclude \n int ex = 0;\n ex += solveUsingMem( root->left , true ,dp);\n ex += solveUsingMem( root->right , true , dp);\n\n ans = max( in , ex );\n\n }\n else\n {\n ans += solveUsingMem( root->left , true , dp );\n ans += solveUsingMem( root->right , true , dp);\n }\n dp[{root , can }] = ans;\n return dp[{root , can }];\n }\n\n\n// Define the unordered_map with the custom hash function\n// std::unordered_map<std::pair<TreeNode*, bool>, int, pair_hash> dp;\n int rob(TreeNode* root) {\n // unordered_map< pair<TreeNode * , bool >, int > dp;\n unordered_map<std::pair<TreeNode*, bool>, int, pair_hash> dp;\n return solveUsingMem( root , true , dp);\n }\n};", "memory": "29681" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n struct pair_hash {\n template <class T1, class T2>\n size_t operator() (const pair<T1, T2>& p) const {\n auto hash1 = hash<T1>{}(p.first);\n auto hash2 = hash<T2>{}(p.second);\n return hash1 ^ (hash2 << 1); \n }\n};\n\nclass Solution {\npublic:\n int solve(TreeNode* root, int canRob, unordered_map<std::pair<TreeNode*, int>, int, pair_hash>& dp){\n if(root == NULL){\n return 0;\n }\n\n if(dp.find({root,canRob}) != dp.end()){\n return dp[{root,canRob}];\n }\n\n int ans1 = 0;\n int ans2 = 0;\n\n if(canRob == 1){\n ans1 = root->val + solve(root->left,0,dp) + solve(root->right,0,dp);\n }\n ans2 = solve(root->left,1,dp) + solve(root->right,1,dp);\n\n dp[{root,canRob}] = max(ans1,ans2);\n\n return dp[{root,canRob}];\n\n }\n int rob(TreeNode* root) {\n // unordered_map<pair<TreeNode*,int>,int> mpp;\n unordered_map<pair<TreeNode*, int>, int, pair_hash> mpp;\n return solve(root,1,mpp);\n }\n};", "memory": "29893" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int solve(TreeNode *root,int r,unordered_map<TreeNode*,pair<int,int>>& dp)\n {\n if (root==0)\n {\n return 0;\n }\n auto i=dp.find(root);\n if (i!=dp.end() and r==i->second.second)\n {\n return i->second.first;\n }\n int a=0,c=0;\n c=solve(root->left,1,dp)+solve(root->right,1,dp);\n if (r==1)\n {\n a=solve(root->left,0,dp)+solve(root->right,0,dp)+root->val;\n }\n dp.insert({root,make_pair(max(a,c),r)});\n return max(a,c);\n }\n int rob(TreeNode* root) {\n unordered_map<TreeNode*,pair<int,int>> dp;\n return solve(root,1,dp);\n }\n};", "memory": "30104" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n\n int solve(TreeNode *root, bool prev, map<pair<TreeNode *, bool>, int> &m) {\n if(!root) return 0;\n if(m.find({root, prev}) != m.end()) return m[{root, prev}];\n if(!prev) {\n return m[{root, prev}] = max(solve(root->left, 0, m) + solve(root->right, 0, m), root->val + solve(root->left, 1, m) + solve(root->right, 1, m));\n }\n return m[{root, prev}] = solve(root->left, 0, m) + solve(root->right, 0, m);\n }\n\n int rob(TreeNode* root) {\n map<pair<TreeNode *, bool>, int> m;\n return solve(root, 0, m);\n }\n};", "memory": "30104" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n map<pair<TreeNode*, int>, int> m;\n int f(TreeNode* root, int j)\n {\n if(!root) return 0;\n\n if(m.find({root, j}) != m.end()) return m[{root, j}];\n\n if(j == 0)\n {\n int ans1 = 0, ans2 = root->val;\n if(root->left)\n {\n ans1 = f(root->left, 0);\n ans2 += f(root->left, 1);\n }\n\n if(root->right)\n {\n ans1 += f(root->right, 0);\n ans2 += f(root->right, 1);\n }\n\n ans1 = max(ans1, ans2);\n return m[{root, j}] = ans1;\n }\n\n if(j == 1)\n {\n int ans = 0;\n\n if(root->left)\n {\n ans = f(root->left, 0);\n }\n\n if(root->right)\n {\n ans += f(root->right, 0);\n }\n \n return m[{root, 1}] = ans;\n }\n\n return 0;\n }\n int rob(TreeNode* root)\n {\n return f(root, 0);\n }\n};", "memory": "30315" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n map<pair<TreeNode*,bool>,int>dp;\n int f(TreeNode* node,int o){\n if(node==nullptr){\n return 0;\n }\n if(dp[{node,o}]!=0){\n return dp[{node,o}];\n }\n if(o==0){\n return dp[{node,o}]=max(node->val+f(node->left,1)+f(node->right,1),f(node->left,0)+f(node->right,0));\n }\n return dp[{node,o}]=f(node->left,0)+f(node->right,0);\n }\npublic:\n int rob(TreeNode* root) {\n return f(root,0);\n }\n};", "memory": "30315" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * long long int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(long long int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(long long int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\nlong long int solve(TreeNode* nd, long long int p, map<pair<long long int, long long int>, long long int> &dp, long long int x){\n if(nd == NULL){\n return 0;\n }\n if(dp[{x, p}] != 0){\n return dp[{x, p}];\n }\n if(p == 1){\n long long int p1 = nd->val + solve(nd->left, 0, dp, (2*x+1)) + solve(nd->right, 0, dp, (2*x+2));\n long long int p2 = solve(nd->left, 1, dp, (2*x+1)) + solve(nd->right, 1, dp, (2*x+2));\n return dp[{x, p}] = max(p1, p2);\n }\n else{\n long long int p1 = solve(nd->left, 1, dp, (2*x+1)) + solve(nd->right, 1, dp, (2*x+2));\n return dp[{x, p}] = p1;\n }\n // long long int p2 = solve(nd->left) + solve(nd->right);\n // return max(p1, p2);\n}\n int rob(TreeNode* root) {\n map<pair<long long int, long long int>, long long int> dp;\n // vector<vector<long long int>> dp(100000, vector<long long int>(2, -1));\n return solve(root, 1, dp, 0);\n \n }\n};", "memory": "30526" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int* houseRobberIII(TreeNode* root){\n if(root == NULL){\n int* empty = new int[2];\n empty[0] = 0;\n empty[1] = 0;\n return empty;\n }\n int* leftAns = new int[2];\n int* rightAns = new int[2];\n leftAns = houseRobberIII(root->left);\n rightAns = houseRobberIII(root->right);\n int* currAns = new int[2];\n currAns[0] = leftAns[1] + root->val + rightAns[1];\n currAns[1] = max(leftAns[0],leftAns[1]) + max(rightAns[0],rightAns[1]);\n\n return currAns; \n }\n int rob(TreeNode* root) {\n int* robberyAmount = new int[2];\n robberyAmount = houseRobberIII(root);\n int maxAmount = max(robberyAmount[0] , robberyAmount[1]);\n return maxAmount;\n }\n};", "memory": "30526" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n auto ans = dfs(root);\n return max(ans[0], ans[1]);\n }\n vector<int> dfs(TreeNode* root) {\n if (!root) return {0, 0};\n auto left = dfs(root->left);\n auto right = dfs(root->right);\n int rob = left[1] + right[1] + root->val;\n int norob = max(left[0], left[1]) + max(right[0], right[1]);\n return {rob, norob};\n }\n};", "memory": "30738" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int>solve(TreeNode* root){\n if(!root){\n return {0,0};\n }\n vector<int> left=solve(root->left);\n vector<int>right=solve(root->right);\n vector<int> options(2);\n options[0]=root->val+left[1]+right[1]; //take\n options[1]=max(left[0],left[1])+max(right[0],right[1]);\n return options;\n }\n int rob(TreeNode* root) {\n vector<int> options(2);\n options=solve(root);\n return max(options[0],options[1]);\n }\n};", "memory": "30949" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int rob(TreeNode* root) {\n vector<int> result = robTree(root);\n return max(result[0],result[1]);\n }\n vector<int> robTree(TreeNode *cur){\n if(cur == NULL) return vector<int> {0,0};\n //后序遍历 左右中\n vector<int> left = robTree(cur->left);\n vector<int> right = robTree(cur->right);\n int val1 = cur->val +left[0] + right[0];\n int val2 = max(left[0],left[1])+ max(right[0],right[1]);\n return {val2,val1}; \n }\n\n};", "memory": "31160" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> robTree(TreeNode* root) {\n if (root == NULL) return vector<int> {0,0};\n vector<int> left = robTree(root->left);\n vector<int> right = robTree(root->right);\n int val1= root->val + left[0] + right[0];\n int val2 = max(left[0], left[1]) + max(right[0], right[1]);\n return {val2, val1};\n }\n int rob(TreeNode* root) {\n vector<int> res = robTree(root);\n return max({res[0], res[1]});\n }\n};", "memory": "31160" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\n #include <cmath>\nclass Solution {\npublic: \n vector<int> f(TreeNode* root)\n {\n if(root==NULL) return {0,0};\n vector<int> l = f(root->left);\n vector<int> r = f(root->right);\n int take = root->val + l[1] + r[1];\n int nottake = max(l[0],l[1])+max(r[1],r[0]);\n return {take,nottake};\n }\n int rob(TreeNode* root) {\n vector<int> ans = f(root);\n return max(ans[0],ans[1]);\n }\n};", "memory": "31371" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n public:\n int rob(TreeNode* root) {\n vector<int> answers = Rob(root);\n return max(answers[0], answers[1]);\n }\n\n private:\n vector<int> Rob(TreeNode* root) {\n if (root == nullptr) return {0, 0};\n vector<int> left = Rob(root->left);\n vector<int> right = Rob(root->right);\n\n return {\n max(left[0], left[1]) + max(right[0], right[1]),\n root->val + left[0] + right[0]\n };\n }\n};\n", "memory": "31371" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> solve(TreeNode* root) {\n vector<int> ans(2, 0);\n\n if(!root) return ans;\n\n vector<int> left=solve(root->left);\n vector<int> right=solve(root->right);\n\n ans[0]= root->val+left[1]+right[1];\n ans[1]= max(left[0], left[1])+max(right[0], right[1]);\n\n return ans;\n }\n int rob(TreeNode* root) {\n vector<int> result = solve(root);\n return max(result[0], result[1]);\n }\n};", "memory": "31583" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n vector<int> dfs(TreeNode* root){\n if(!root)\n return vector<int>(2, 0);\n \n vector<int> left = dfs(root->left);\n vector<int> right = dfs(root->right);\n vector<int> dp(2, 0);\n\n dp[0] = max(left[0], left[1]) + max(right[0], right[1]);\n dp[1] = root->val + left[0] + right[0];\n\n return dp;\n\n }\n\n int rob(TreeNode* root) {\n vector<int> dp = dfs(root);\n\n return max(dp[0], dp[1]);\n }\n};", "memory": "31583" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n int dp[1000005][2];\n map<int, int>mp;\n int count1 = 0;\n int DP(TreeNode* root, int node, int takenParent)\n {\n if(dp[node][takenParent]!=-1) return dp[node][takenParent];\n int val = 0, val1 = 0;\n if(!takenParent){\n val = mp[root->val];\n if(root->left != NULL) val += DP(root->left, root->left->val, 1);\n if(root->right != NULL) val += DP(root->right, root->right->val, 1);\n }\n if(root->left != NULL) val1 += DP(root->left, root->left->val, 0);\n if(root->right != NULL) val1 += DP(root->right, root->right->val, 0);\n return dp[node][takenParent] = max(val, val1);\n }\n void DFS(TreeNode* root)\n {\n if(root==NULL) return;\n mp[count1] = root->val;\n root->val = count1;\n count1 += 1;\n DFS(root->left);\n DFS(root->right);\n }\n\n int rob(TreeNode* root) {\n DFS(root);\n memset(dp, -1, sizeof(dp));\n return DP(root, 0, 0);\n }\n};", "memory": "31794" }
337
<p>The thief has found himself a new place for his thievery again. There is only one entrance to this area, called <code>root</code>.</p> <p>Besides the <code>root</code>, each house has one and only one parent house. After a tour, the smart thief realized that all houses in this place form a binary tree. It will automatically contact the police if <strong>two directly-linked houses were broken into on the same night</strong>.</p> <p>Given the <code>root</code> of the binary tree, return <em>the maximum amount of money the thief can rob <strong>without alerting the police</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob1-tree.jpg" style="width: 277px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,2,3,null,3,null,1] <strong>Output:</strong> 7 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2021/03/10/rob2-tree.jpg" style="width: 357px; height: 293px;" /> <pre> <strong>Input:</strong> root = [3,4,5,1,3,null,1] <strong>Output:</strong> 9 <strong>Explanation:</strong> Maximum amount of money the thief can rob = 4 + 5 = 9. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is in the range <code>[1, 10<sup>4</sup>]</code>.</li> <li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li> </ul>
3
{ "code": "/**\n * Definition for a binary tree node.\n * struct TreeNode {\n * int val;\n * TreeNode *left;\n * TreeNode *right;\n * TreeNode() : val(0), left(nullptr), right(nullptr) {}\n * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\n \n int rob(TreeNode* root, bool prev, unordered_map<string, int>& cache) {\n \n if(!root) {\n return 0;\n }\n std::stringstream ss;\n ss << root;\n std::string key = ss.str();\n key += to_string(prev);\n if(cache.find(key) != cache.end()) {\n return cache[key];\n }\n int rob_val = 0;\n if(!prev) {\n rob_val = root->val + rob(root->left, true, cache) + rob(root->right, true, cache);\n }\n int not_rob = rob(root->left, false, cache) + rob(root->right, false, cache);\n \n cache[key] = max(rob_val, not_rob);\n return max(rob_val, not_rob);\n }\n \npublic:\n int rob(TreeNode* root) {\n unordered_map<string, int> cache;\n return rob(root, false, cache);\n }\n};", "memory": "31794" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\nprivate:\n static constexpr uint MAXN = 100'000u;\n static constexpr uint8_t BIAS = 17;\n static constexpr uint BMASK = (1u << BIAS) - 1u;\n static uint64_t p2i[MAXN];\n\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, const string &directions) {\n const uint n = positions.size();\n for (uint i = 0; i < n; i++)\n p2i[i] = (uint64_t(positions[i]) << BIAS) + i;\n sort(p2i, p2i + n, greater());\n\n uint st[n], send = 0;\n for (uint k = 0; k < n; k++) {\n const uint i = p2i[k] & BMASK;\n if (directions[i] == 'L') {\n st[send++] = i;\n } else {\n uint hi = healths[i];\n while (send && hi) {\n const uint j = st[send-1u], hj = healths[j];\n send -= hj <= hi;\n healths[j] = hj > hi ? hj - 1u : 0;\n hi = hj < hi ? hi - 1u : 0;\n }\n healths[i] = hi;\n }\n }\n\n uint i = 0;\n for ( ; i < n && healths[i]; i++)\n ;\n uint j = i++;\n for ( ; i < n; i++) {\n const uint h = healths[i];\n healths[j] = h;\n j += !!h;\n }\n healths.resize(j);\n return move(healths);\n }\n};\n\nuint64_t Solution::p2i[MAXN];\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();", "memory": "189258" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\nprivate:\n static constexpr uint MAXN = 100'000u;\n static constexpr uint8_t BIAS = 17;\n static constexpr uint BMASK = (1u << BIAS) - 1u;\n static uint64_t p2i[MAXN];\n\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, const string &directions) {\n const uint n = positions.size();\n for (uint i = 0; i < n; i++)\n p2i[i] = (uint64_t(positions[i]) << BIAS) + i;\n sort(p2i, p2i + n, greater());\n\n uint st[n], send = 0;\n for (uint k = 0; k < n; k++) {\n const uint i = p2i[k] & BMASK;\n if (directions[i] == 'L') {\n st[send++] = i;\n } else {\n uint hi = healths[i];\n while (send && hi) {\n const uint j = st[send-1u], hj = healths[j];\n send -= hj <= hi;\n healths[j] = hj > hi ? hj - 1u : 0;\n hi = hj < hi ? hi - 1u : 0;\n }\n healths[i] = hi;\n }\n }\n\n uint i = 0;\n for ( ; i < n && healths[i]; i++)\n ;\n uint j = i++;\n for ( ; i < n; i++) {\n const uint h = healths[i];\n healths[j] = h;\n j += !!h;\n }\n healths.resize(j);\n return move(healths);\n }\n};\n\nuint64_t Solution::p2i[MAXN];\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();", "memory": "189258" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int> &positions, vector<int> &healths, string directions) {\n int n = positions.size(), id[n];\n iota(id, id + n, 0);\n sort(id, id + n, [&](const int i, const int j) {\n return positions[i] < positions[j];\n });\n\n stack<int> st;\n for (int i: id) {\n if (directions[i] == 'R') { // 向右,存入栈中\n st.push(i);\n continue;\n }\n // 向左,与栈中向右的机器人碰撞\n while (!st.empty()) {\n int top = st.top();\n if (healths[top] > healths[i]) { // 栈顶的健康度大\n healths[top]--;\n healths[i] = 0;\n break;\n }\n if (healths[top] == healths[i]) { // 健康度一样大\n healths[top] = 0;\n st.pop(); // 移除栈顶\n healths[i] = 0;\n break;\n }\n healths[top] = 0;\n st.pop(); // 移除栈顶\n healths[i]--; // 当前机器人的健康度大\n }\n }\n\n // 去掉 0\n healths.erase(remove(healths.begin(), healths.end(), 0), healths.end());\n return healths;\n }\n};\n\n", "memory": "190576" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "/*\n Summary\n ------------------------------------\n O(nlogn): sort and simulation\n ------------------------------------\n*/\nint idx[100000], buf[100000];\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int n=positions.size(), m=-1;\n const char * c=directions.c_str();\n vector<int> ans;\n iota(idx, idx+n, 0);\n sort(idx, idx+n, [&](int a, int b) {\n return positions[a]<positions[b];\n });\n for(int i=0, j; i<n; ) {\n j=idx[i++];\n if(c[j]=='L') {\n while(m>=0) {\n if(healths[j]>healths[buf[m]]) {\n --healths[j];\n healths[buf[m--]]=0;\n } else if(healths[j]<healths[buf[m]]) {\n healths[j]=0;\n --healths[buf[m]];\n break;\n } else {\n healths[j]=healths[buf[m--]]=0;\n break;\n }\n }\n } else {\n buf[++m]=j;\n }\n }\n for(int i=0; i<n; ++i) {\n if(healths[i])\n ans.emplace_back(healths[i]);\n }\n return ans;\n }\n};", "memory": "190576" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& p, vector<int>& h, string d) {\n int n=p.size();\n pair<int,int> pi[n];\n for(int i=0;i<n;i++) pi[i]=make_pair(p[i],i);\n sort(pi,pi+n);\n vector<int> s;\n for(int i=0;i<n;i++) {\n if(s.empty() || d[s.back()]!='R' || d[pi[i].second]!='L')\n s.push_back(pi[i].second);\n else if (h[s.back()]>h[pi[i].second])\n h[s.back()]--;\n else if (h[s.back()]<h[pi[i].second])\n s.pop_back(),\n h[pi[i].second]--,\n i--;\n else s.pop_back();\n }\n sort(s.begin(),s.end());\n for(int &i:s) i=h[i];\n return s;\n }\n};", "memory": "191893" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& p, vector<int>& h, string d) {\n int n=p.size();\n pair<int,int> pi[n];\n for(int i=0;i<n;i++) pi[i]=make_pair(p[i],i);\n sort(pi,pi+n);\n vector<int> s;\n for(int i=0;i<n;i++) {\n if(s.empty() || d[s.back()]!='R' || d[pi[i].second]!='L')\n s.push_back(pi[i].second);\n else if (h[s.back()]>h[pi[i].second])\n h[s.back()]--;\n else if (h[s.back()]<h[pi[i].second])\n s.pop_back(),\n h[pi[i].second]--,\n i--;\n else s.pop_back();\n }\n sort(s.begin(),s.end());\n for(int &i:s) i=h[i];\n return s;\n }\n};", "memory": "191893" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<int> idxs(size(positions));\n iota(begin(idxs), end(idxs), 0);\n sort(begin(idxs), end(idxs), [&](const auto& a, const auto& b) {\n return positions[a] < positions[b];\n });\n\n vector<int> stk;\n stk.reserve(size(idxs));\n for (const auto& i : idxs) {\n if (directions[i] == 'R') {\n stk.emplace_back(i);\n continue;\n }\n while (!empty(stk)) {\n if (healths[stk.back()] == healths[i]) {\n healths[stk.back()] = healths[i] = 0;\n stk.pop_back();\n break;\n }\n if (healths[stk.back()] > healths[i]) {\n healths[i] = 0;\n --healths[stk.back()];\n break;\n } \n healths[stk.back()] = 0;\n --healths[i];\n stk.pop_back();\n }\n }\n \n vector<int> result;\n result.reserve(size(healths));\n for (const auto& x : healths) {\n if (x) {\n result.emplace_back(x);\n }\n }\n return result;\n }\n};", "memory": "193211" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n std::vector<size_t> idx(positions.size());\n std::iota(idx.begin(), idx.end(), 0);\n\n std::sort(idx.begin(), idx.end(), [&positions](size_t i1, size_t i2) {\n return positions[i1] < positions[i2];\n });\n static constexpr int minHealth = INT_MIN;\n auto findNext = [&idx, &healths, &directions](char what, int start) {\n for (size_t i = start; i < idx.size(); i++) {\n int r1 = idx[i];\n if (directions[r1] == what && healths[r1] != minHealth)\n return i;\n }\n return idx.size();\n };\n\n size_t firstR = 0;\n firstR = findNext('R', 0);\n size_t nextL = firstR + 1;\n while (firstR < idx.size()) {\n nextL = findNext('L', nextL);\n if (nextL == idx.size())\n break;\n\n size_t lastR = nextL - 1;\n while (healths[idx[lastR]] == minHealth && lastR > firstR)\n lastR--;\n int r = idx[lastR];\n int l = idx[nextL];\n\n while (healths[l] > healths[r]) {\n healths[l]--;\n healths[r] = minHealth;\n if (lastR == firstR)\n break;\n while (healths[idx[lastR]] == minHealth && lastR > firstR)\n lastR--;\n r = idx[lastR];\n }\n if (healths[l] == healths[r]) {\n healths[l] = minHealth;\n healths[r] = minHealth;\n }\n while (healths[l] < healths[r]) {\n healths[l] = minHealth;\n healths[r]--;\n nextL++;\n if (nextL == idx.size())\n break;\n l = idx[nextL];\n if (directions[l] != 'L')\n break;\n }\n if (lastR == firstR && healths[r] == minHealth) {\n firstR = findNext('R', nextL);\n nextL = firstR + 1;\n }\n }\n healths.erase(std::remove_if(healths.begin(), healths.end(),\n [](int h) { return h == minHealth; }),\n healths.end());\n return healths;\n }\n};", "memory": "193211" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& h, string directions) {\n int n = positions.size();\n vector<int> ind(n), stack, res;\n for (int i = 0; i < n; i++)\n ind[i] = i;\n sort(ind.begin(), ind.end(), [&](int a, int b) {\n return positions[a] < positions[b];\n });\n for (int i : ind) {\n if (directions[i] == 'R') {\n stack.push_back(i);\n continue;\n }\n while (!stack.empty() && h[i] > 0) {\n if (h[stack.back()] < h[i]) {\n h[stack.back()] = 0, stack.pop_back();\n h[i] -= 1;\n } else if (h[stack.back()] > h[i]) {\n h[stack.back()] -= 1;\n h[i] = 0;\n } else {\n h[stack.back()] = 0, stack.pop_back();\n h[i] = 0;\n }\n }\n }\n for (int v : h) {\n if (v > 0) {\n res.push_back(v);\n }\n }\n return res;\n }\n};", "memory": "194528" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<int> index(n), stack, res;\n for (int i = 0; i < n; i++)\n index[i] = i;\n sort(index.begin(), index.end(), [&](int a, int b) {\n return positions[a] < positions[b];\n });\n for (int i : index) {\n if (directions[i] == 'R') {\n stack.push_back(i);\n continue;\n }\n while (!stack.empty() && healths[i] > 0) {\n if (healths[stack.back()] < healths[i]) {\n healths[stack.back()] = 0, stack.pop_back();\n healths[i] -= 1;\n } else if (healths[stack.back()] > healths[i]) {\n healths[stack.back()] -= 1;\n healths[i] = 0;\n } else {\n healths[stack.back()] = 0, stack.pop_back();\n healths[i] = 0;\n }\n }\n }\n for (int v : healths) {\n if (v > 0) {\n res.push_back(v);\n }\n }\n return res;\n }\n};", "memory": "194528" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n const int n = positions.size();\n vector<int> ind(n);\n deque<int> right;\n vector<int> ans;\n\n iota(ind.begin(), ind.end(), 0);\n \n sort(ind.begin(), ind.end(), [&](int a, int b) {\n return positions[a] < positions[b];\n });\n\n for (int idx: ind) {\n if (directions[idx] == 'L') {\n while (!right.empty() && healths[right.back()] < healths[idx]) {\n right.pop_back();\n healths[idx]--;\n }\n\n if (right.empty()) {\n ans.push_back(idx);\n } else if (healths[right.back()] != healths[idx]) {\n healths[right.back()]--;\n } else {\n right.pop_back();\n }\n } else {\n right.push_back(idx);\n }\n }\n\n for (int r: right) {\n ans.push_back(r);\n }\n\n sort(ans.begin(), ans.end());\n\n for (int i = 0;i < ans.size();i++) {\n ans[i] = healths[ans[i]];\n }\n\n return ans;\n }\n};", "memory": "195846" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "auto init = []()\n{ \n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n const int n = positions.size();\n vector<int> ind(n);\n stack<int> right;\n vector<int> ans;\n\n iota(ind.begin(), ind.end(), 0);\n \n sort(ind.begin(), ind.end(), [&](int a, int b) {\n return positions[a] < positions[b];\n });\n\n for (int idx: ind) {\n if (directions[idx] == 'L') {\n while (!right.empty() && healths[right.top()] < healths[idx]) {\n right.pop();\n healths[idx]--;\n }\n\n if (right.empty()) {\n ans.push_back(idx);\n } else if (healths[right.top()] != healths[idx]) {\n healths[right.top()]--;\n } else {\n right.pop();\n }\n } else {\n right.push(idx);\n }\n }\n\n while (!right.empty()) {\n ans.push_back(right.top());\n right.pop();\n }\n\n sort(ans.begin(), ans.end());\n\n for (int i = 0;i < ans.size();i++) {\n ans[i] = healths[ans[i]];\n }\n\n return ans;\n }\n};", "memory": "195846" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n int size = positions.size();\n vector<pair<int, int>> pv;\n deque<int> dq; // (index)\n vector<int> res;\n\n pv.reserve(size);\n for (auto i = 0; i < size; i++) {\n pv.emplace_back(positions[i], i);\n }\n sort(pv.begin(), pv.end(),\n [](const auto& a, const auto& b) { return a.first < b.first; });\n\n for (auto i = 0; i < size; i++) {\n auto pos = pv[i].second;\n auto& health = healths[pos];\n if (directions[pos] == 'R') {\n dq.emplace_back(pos);\n continue;\n }\n\n while (!dq.empty() && health) {\n if (auto& t = healths[dq.back()]; health > t) {\n health--;\n t = 0;\n dq.pop_back();\n } else if (health < t) {\n t--;\n health = 0;\n } else {\n t = 0;\n health = 0;\n dq.pop_back();\n }\n }\n }\n\n res.reserve(size);\n for (const auto& health : healths) {\n if (health) {\n res.emplace_back(health);\n }\n }\n return res;\n }\n};", "memory": "197163" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
0
{ "code": "class Solution {\n struct Robot {\n int idx;\n int pos;\n int health;\n char dir;\n\n Robot(int id, int a, int b, char c) {\n idx = id;\n pos = a;\n health = b;\n dir = c;\n }\n };\n\n static bool compPos(Robot &a, Robot &b) {\n return a.pos < b.pos;\n }\n static bool compIdx(Robot &a, Robot &b) {\n return a.idx < b.idx;\n }\n\n vector<Robot> myv;\n\n bool checkDir(string str) {\n int r=0,l=0;\n for(char c: str) {\n (c=='R') ? r++ : l++;\n }\n return (r==0) || (l==0);\n }\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n if (n == 1 || checkDir(directions)) {\n return healths;\n }\n myv.reserve(positions.size());\n for(int i=0;i<positions.size(); i++) {\n myv.push_back(Robot(i, positions[i], healths[i], directions[i]));\n }\n sort(myv.begin(), myv.end(), compPos);\n\n int i = 1;\n while(i<myv.size()) {\n if (myv[i-1].dir == 'R' && myv[i].dir == 'L') {\n if (myv[i-1].health == myv[i].health) {\n myv.erase(myv.begin()+(i-1));\n myv.erase(myv.begin()+(i-1));\n } else if (myv[i-1].health > myv[i].health) {\n myv[i-1].health--;\n myv.erase(myv.begin()+i);\n } else {\n myv[i].health--;\n myv.erase(myv.begin()+(i-1));\n }\n if (i>1) {i--;}\n continue;\n }\n i++;\n }\n sort(myv.begin(), myv.end(),compIdx);\n\n vector<int> ans;\n ans.reserve(myv.size());\n for(auto r: myv) {\n ans.push_back(r.health);\n }\n return ans;\n }\n};", "memory": "197163" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int size = positions.size();\n vector<pair<int, int>> pos(size);\n for (int i = 0; i < size; i++) {\n pos[i] = {positions[i], i};\n }\n sort(pos.begin(), pos.end());\n stack<int> st;\n\n for (const auto& p : pos) {\n while (!st.empty() && has_collision(directions, st.top(), p.second)) {\n if (healths[st.top()] == healths[p.second]) {\n healths[st.top()] = healths[p.second] = 0;\n st.pop();\n break;\n } else if (healths[st.top()] > healths[p.second]) {\n healths[st.top()]--;\n healths[p.second] = 0;\n break;\n } else {\n healths[st.top()] = 0;\n healths[p.second]--;\n st.pop();\n }\n }\n if (healths[p.second] > 0) {\n st.push(p.second);\n }\n \n }\n\n vector<int> res;\n for (int n : healths) if (n > 0) res.push_back(n);\n\n return res;\n }\n\n bool has_collision(const string& dir, int i, int j) {\n if (dir[i] == 'R' && dir[j] == 'L') return true;\n return false;\n }\n};", "memory": "198481" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<pair<int, int>> robots(n);\n for (int i = 0; i < n; i++) {\n robots[i] = {positions[i], i};\n }\n sort(robots.begin(), robots.end());\n\n stack<int> st;\n vector<int> res;\n for (auto& robot : robots) {\n int idx = robot.second;\n if (directions[idx] == 'R') {\n st.push(idx);\n } else {\n while (!st.empty() && directions[st.top()] == 'R' && healths[idx] > 0) {\n int idx2 = st.top();\n st.pop();\n if (healths[idx] > healths[idx2]) {\n healths[idx] --;\n healths[idx2] = 0;\n } else if (healths[idx] < healths[idx2]) {\n healths[idx2] --;\n healths[idx] = 0;\n st.push(idx2);\n } else {\n healths[idx] = 0;\n healths[idx2] = 0;\n }\n }\n if (healths[idx] > 0) st.push(idx);\n }\n }\n\n for (int i = 0; i < n; i++) {\n if (healths[i] > 0) res.push_back(healths[i]);\n }\n return res;\n }\n};", "memory": "198481" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<int> idxs;\n for (int i=0;i<positions.size();++i) {\n idxs.push_back(i);\n }\n sort (idxs.begin(),\n idxs.end(),\n [&positions](const int& a, const int& b) {return positions[a] <= positions[b];});\n\n stack<int> rem;\n\n for (int i=0;i<idxs.size();++i) {\n if (directions[idxs[i]] == 'R' ||\n rem.empty() ||\n directions[rem.top()] == 'L') {\n rem.push(idxs[i]);\n }\n else {\n // curr movement left and top movement right\n bool add_new = true;\n while (!rem.empty() && directions[rem.top()] == 'R') {\n int prev = rem.top();\n if (healths[prev] > healths[idxs[i]]) {\n healths[prev] -= 1;\n add_new = false;\n break;\n }\n else if (healths[prev] == healths[idxs[i]]) {\n rem.pop();\n add_new = false;\n break;\n }\n else {\n healths[idxs[i]] -= 1;\n rem.pop();\n }\n }\n if (add_new) {\n rem.push(idxs[i]);\n }\n }\n }\n vector<int> final_res;\n while (!rem.empty()) {\n final_res.push_back(rem.top());\n rem.pop();\n }\n sort(final_res.begin(), final_res.end());\n for (int i=0;i<final_res.size();++i)\n final_res[i] = healths[final_res[i]];\n return final_res;\n }\n};", "memory": "199798" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<int> idx;\n for(int i = 0; i < n; i++) idx.push_back(i);\n sort(idx.begin(), idx.end(), [&](int a, int b) {\n return positions[a] < positions[b];\n });\n stack<int> st;\n for(int i: idx) {\n while(!st.empty() && directions[st.top()] == 'R' && directions[i] == 'L') {\n if (healths[st.top()] < healths[i]) {\n healths[st.top()] = -1;\n healths[i]--;\n st.pop();\n }\n else {\n if (healths[st.top()] > healths[i]) healths[st.top()]--;\n else {\n healths[st.top()] = -1;\n st.pop();\n }\n healths[i] = -1;\n break;\n }\n }\n if (healths[i] > 0) st.push(i);\n }\n vector<int> ans;\n for(int a: healths) {\n if (a > 0) ans.push_back(a);\n }\n return ans;\n }\n};", "memory": "199798" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<int> indices;\n int len = positions.size();\n vector<bool> alive(len, true);\n for (int i{0}; i < len; i++) {\n indices.push_back(i);\n }\n sort(indices.begin(), indices.end(), [&positions](int i, int j) {\n return positions.at(i) < positions.at(j);\n });\n vector<int> robotStk{indices.at(0)};\n for (int i{1}; i < len; i++) {\n if (robotStk.empty()) {\n robotStk.push_back(indices.at((i)));\n continue;\n }\n int firstIndex{robotStk.back()}, secondIndex{indices.at(i)};\n char firstDir{directions.at(firstIndex)}, secondDir{directions.at(secondIndex)};\n if (firstDir == secondDir || firstDir == 'L' && secondDir == 'R') {\n robotStk.push_back(secondIndex);\n }\n else {\n int firstHealth{healths.at(firstIndex)}, secondHealth{healths.at(secondIndex)};\n if (firstHealth == secondHealth) {\n robotStk.pop_back();\n alive.at(firstIndex) = false;\n alive.at(secondIndex) = false;\n }\n else if (firstHealth < secondHealth) {\n robotStk.pop_back();\n robotStk.push_back(secondIndex);\n alive.at(firstIndex) = false;\n healths.at(secondIndex)--;\n while (robotStk.size() > 1) {\n int firstIndex{robotStk.at(robotStk.size()-2)}, secondIndex{robotStk.back()};\n char firstDir{directions.at(firstIndex)}, secondDir{directions.at(secondIndex)};\n if (firstDir == 'R' && secondDir == 'L') {\n int firstHealth{healths.at(firstIndex)}, secondHealth{healths.at(secondIndex)};\n if (firstHealth == secondHealth) {\n robotStk.pop_back();\n robotStk.pop_back();\n alive.at(firstIndex) = false;\n alive.at(secondIndex) = false;\n }\n else if (firstHealth < secondHealth) {\n robotStk.pop_back();\n robotStk.pop_back();\n robotStk.push_back(secondIndex);\n alive.at(firstIndex) = false;\n healths.at(secondIndex)--;\n }\n else {\n robotStk.pop_back();\n alive.at(secondIndex) = false;\n healths.at(firstIndex)--;\n }\n }\n else {\n break;\n }\n }\n }\n else {\n alive.at(secondIndex) = false;\n healths.at(firstIndex)--;\n }\n }\n }\n /*\n while (robotStk.size() > 1) {\n int firstIndex{robotStk.at(robotStk.size()-2)}, secondIndex{robotStk.back()};\n char firstDir{directions.at(firstIndex)}, secondDir{directions.at(secondIndex)};\n if (firstDir == 'R' && secondDir == 'L') {\n int firstHealth{healths.at(firstIndex)}, secondHealth{healths.at(secondIndex)};\n if (firstHealth == secondHealth) {\n robotStk.pop_back();\n robotStk.pop_back();\n alive.at(firstIndex) = false;\n alive.at(secondIndex) = false;\n }\n else if (firstHealth < secondHealth) {\n robotStk.pop_back();\n robotStk.pop_back();\n robotStk.push_back(secondIndex);\n alive.at(firstIndex) = false;\n healths.at(secondIndex)--;\n }\n else {\n robotStk.pop_back();\n alive.at(secondIndex) = false;\n healths.at(firstIndex)--;\n }\n }\n else {\n break;\n }\n }\n */\n vector<int> ans;\n for (int i{0}; i < len; i++) {\n if (alive.at(i)) {\n ans.push_back(healths.at(i));\n }\n }\n return ans;\n }\n};", "memory": "201116" }