{"id":10232,"date":"2025-03-27T21:24:17","date_gmt":"2025-03-28T04:24:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10232"},"modified":"2025-03-27T21:25:59","modified_gmt":"2025-03-28T04:25:59","slug":"leetcode-3462-maximum-sum-with-at-most-k-elements","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-3462-maximum-sum-with-at-most-k-elements\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 3462. Maximum Sum With at Most K Elements"},"content":{"rendered":"\n<p>\u672c\u9898\u4f7f\u7528\u4e86\u4e24\u6b21 partial sorting \/ std::nth_element \u6765\u8fdb\u884c\u90e8\u5206\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u7ecf\u5178\u6559\u7a0b\u4e86\u3002<\/p>\n\n\n\n<p>\u901f\u5ea6\u6bd4\u5168\u6392\u5e8f\u6216\u8005heap\/pq\u5feb\u5230\u4e0d\u77e5\u9053\u54ea\u91cc\u53bb\u4e86\uff5e<\/p>\n\n\n\n<p>Time complexity: O(m*n)<br>Space complexity: O(m*n)<\/p>\n\n\n\n<p>\u4e0d\u7528\u9ed1\u79d1\u6280\u5c31\u8fbe\u523099.77%<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-27-at-9.19.52\u202fPM.png\"><img loading=\"lazy\" decoding=\"async\" width=\"815\" height=\"448\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-27-at-9.19.52\u202fPM.png\" alt=\"\" class=\"wp-image-10233\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-27-at-9.19.52\u202fPM.png 815w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-27-at-9.19.52\u202fPM-300x165.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2025\/03\/Screenshot-2025-03-27-at-9.19.52\u202fPM-768x422.png 768w\" sizes=\"auto, (max-width: 815px) 100vw, 815px\" \/><\/a><\/figure>\n\n\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  long long maxSum(vector<vector<int>>& grid, vector<int>& limits, int k) {\n    \/\/ Step 1: Sort each row using fast parition, collect largest limits[i] elements.\n    vector<int> nums;\n    nums.reserve(grid.size() * grid[0].size());\n    for (int i = 0; i < grid.size(); ++i) {\n      nth_element(begin(grid[i]), begin(grid[i]) + limits[i], end(grid[i]), std::greater{});\n      nums.insert(nums.end(), begin(grid[i]), begin(grid[i]) + limits[i]);\n    }\n\n    \/\/ Setp 2: fast partition to find the largest k elements. O(m*n).\n    nth_element(begin(nums), begin(nums) + k, end(nums), std::greater{});\n    \n    \/\/ Step 3: Sum them up. O(k)\n    return accumulate(begin(nums), begin(nums) + k, 0LL);\n  }\n};\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u9898\u4f7f\u7528\u4e86\u4e24\u6b21 partial sorting \/ std::nth_element \u6765\u8fdb\u884c\u90e8\u5206\u6392\u5e8f\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u7ecf\u5178\u6559\u7a0b\u4e86\u3002 \u901f\u5ea6\u6bd4\u5168\u6392\u5e8f\u6216\u8005heap\/pq\u5feb\u5230\u4e0d\u77e5\u9053\u54ea\u91cc\u53bb\u4e86\uff5e Time complexity: O(m*n)Space complexity: O(m*n) \u4e0d\u7528\u9ed1\u79d1\u6280\u5c31\u8fbe\u523099.77% class Solution { public: long long maxSum(vector&#038; grid, vector&#038; limits, int k)&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[831,832,23],"class_list":["post-10232","post","type-post","status-publish","format-standard","hentry","category-array","tag-fast-parition","tag-partial-sorting","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=10232"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10232\/revisions"}],"predecessor-version":[{"id":10235,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10232\/revisions\/10235"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}