commit 8e6e601a4ca0c1187af15461d397c428f30fb431 parent 30e288026d578095e0352f474bcf7b547e444ed9 Author: Dimitrije Dobrota <mail@dimitrijedobrota.com> Date: Fri, 3 May 2024 12:52:51 +0200 1 Random Problem Diffstat:
A | Problems/0337.cpp | | | 14 | ++++++++++++++ |
M | README.md | | | 1 | + |
2 files changed, 15 insertions(+), 0 deletions(-)
diff --git a/Problems/0337.cpp b/Problems/0337.cpp @@ -0,0 +1,14 @@ +class Solution { + public: + int rob(TreeNode *root) const { + if (!root) return 0; + if (root->val < 0) return -root->val; + + int res = root->val; + if (root->left) res += rob(root->left->left) + rob(root->left->right); + if (root->right) res += rob(root->right->left) + rob(root->right->right); + + root->val = -max(res, rob(root->left) + rob(root->right)); + return -root->val; + } +}; diff --git a/README.md b/README.md @@ -276,6 +276,7 @@ for solving problems. | 0328 | Medium | [Odd Even Linked List](Problems/0328.cpp) | | 0332 | Hard | [Reconstruct Itinerary](Problems/0332.cpp) | | 0334 | Medium | [Increasing Triplet Subsequence](Problems/0334.cpp) | +| 0337 | Medium | [House Robber III](Problems/0337.cpp) | | 0338 | Easy | [Counting Bits](Problems/0338.cpp) | | 0341 | Medium | [Flatten Nested List Iterator](Problems/0341.cpp) | | 0342 | Easy | [Power of Four](Problems/0342.cpp) |