commit 63482570ce27238d350bf5ffbc472981ccf45eb8
parent 531e5766e93dcfd2527221a5296382e575d67cf7
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Tue, 22 Oct 2024 10:15:50 +0200
Daily Problem
Diffstat:
2 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/Problems/2583.cpp b/Problems/2583.cpp
@@ -0,0 +1,24 @@
+class Solution {
+ public:
+ long long kthLargestLevelSum(const TreeNode *root, int k) const {
+ priority_queue<long long, vector<long long>, greater<>> pq;
+ queue<const TreeNode *> q;
+
+ for (q.emplace(root); !q.empty();) {
+ long long sum = 0;
+ for (int k = size(q); k > 0; k--) {
+ const auto root = q.front();
+ q.pop();
+ sum += root->val;
+
+ if (root->left) q.push(root->left);
+ if (root->right) q.push(root->right);
+ }
+
+ pq.emplace(sum);
+ if (size(pq) > k) pq.pop();
+ }
+
+ return size(pq) == k ? pq.top() : -1;
+ }
+};
diff --git a/README.md b/README.md
@@ -1278,6 +1278,7 @@ for solving problems.
| 2571 | Medium | [Minimum Operations to Reduce an Integer to 0](Problems/2571.cpp) |
| 2579 | Medium | [Count Total Number of Colored Cells](Problems/2579.cpp) |
| 2582 | Easy | [Pass the Pillow](Problems/2582.cpp) |
+| 2583 | Medium | [Kth Largest Sum in a Binary Tree](Problems/2583.cpp) |
| 2588 | Medium | [Count the Number of Beautiful Subarrays](Problems/2588.cpp) |
| 2592 | Medium | [Maximize Greatness of an Array](Problems/2592.cpp) |
| 2593 | Medium | [Find Score of an Array After Marking All Elements](Problems/2593.cpp) |