<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>partial sorting Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/partial-sorting/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/partial-sorting/</link>
	<description></description>
	<lastBuildDate>Fri, 28 Mar 2025 04:25:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.9</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>partial sorting Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/partial-sorting/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 3462. Maximum Sum With at Most K Elements</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-3462-maximum-sum-with-at-most-k-elements/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-3462-maximum-sum-with-at-most-k-elements/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 28 Mar 2025 04:24:17 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[fast parition]]></category>
		<category><![CDATA[partial sorting]]></category>
		<category><![CDATA[sort]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10232</guid>

					<description><![CDATA[<p>本题使用了两次 partial sorting / std::nth_element 来进行部分排序，可以作为经典教程了。 速度比全排序或者heap/pq快到不知道哪里去了～ Time complexity: O(m*n)Space complexity: O(m*n) 不用黑科技就达到99.77% [crayon-67e6da6273ed9848799077/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-3462-maximum-sum-with-at-most-k-elements/">花花酱 LeetCode 3462. Maximum Sum With at Most K Elements</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>本题使用了两次 partial sorting / std::nth_element 来进行部分排序，可以作为经典教程了。</p>



<p>速度比全排序或者heap/pq快到不知道哪里去了～</p>



<p>Time complexity: O(m*n)<br>Space complexity: O(m*n)</p>



<p>不用黑科技就达到99.77%</p>



<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 PM.png"><img width="815" height="448" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2025/03/Screenshot-2025-03-27-at-9.19.52 PM.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 PM.png 815w, https://zxi.mytechroad.com/blog/wp-content/uploads/2025/03/Screenshot-2025-03-27-at-9.19.52 PM-300x165.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2025/03/Screenshot-2025-03-27-at-9.19.52 PM-768x422.png 768w" sizes="(max-width: 815px) 100vw, 815px" /></a></figure>



<pre class="crayon-plain-tag">class Solution {
public:
  long long maxSum(vector&lt;vector&lt;int&gt;&gt;&amp; grid, vector&lt;int&gt;&amp; limits, int k) {
    // Step 1: Sort each row using fast parition, collect largest limits[i] elements.
    vector&lt;int&gt; nums;
    nums.reserve(grid.size() * grid[0].size());
    for (int i = 0; i &lt; grid.size(); ++i) {
      nth_element(begin(grid[i]), begin(grid[i]) + limits[i], end(grid[i]), std::greater{});
      nums.insert(nums.end(), begin(grid[i]), begin(grid[i]) + limits[i]);
    }

    // Setp 2: fast partition to find the largest k elements. O(m*n).
    nth_element(begin(nums), begin(nums) + k, end(nums), std::greater{});
    
    // Step 3: Sum them up. O(k)
    return accumulate(begin(nums), begin(nums) + k, 0LL);
  }
};</pre>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-3462-maximum-sum-with-at-most-k-elements/">花花酱 LeetCode 3462. Maximum Sum With at Most K Elements</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-3462-maximum-sum-with-at-most-k-elements/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
