<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Heap on Yu-Cheng's Notes</title><link>https://linkingmon.github.io/blog/tags/heap/</link><description>Recent content in Heap on Yu-Cheng's Notes</description><generator>Hugo -- 0.166.0</generator><language>en-us</language><lastBuildDate>Fri, 23 Jan 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://linkingmon.github.io/blog/tags/heap/index.xml" rel="self" type="application/rss+xml"/><item><title>LC 3510 - Minimum Pair Removal to Sort Array II</title><link>https://linkingmon.github.io/blog/coding/lc-3510/</link><pubDate>Fri, 23 Jan 2026 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-3510/</guid><description>&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;This is a raelly interesting problem. same problem with yesterdays daily but need to come up with a much faster method.&lt;/p&gt;
&lt;p&gt;I was originally thinking that checking a vector to be sorted is &lt;code&gt;O(N)&lt;/code&gt;, and we can only done in &lt;code&gt;O(N^2)&lt;/code&gt;, and after reading one solution, I find that we only need to check the neigobers order, and count the number of ordered pairs. In this case, we can check O(1) to update the number of ordered pairs, and this can be done in &lt;code&gt;O(1)&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>LC 23 - Merge k Sorted Lists</title><link>https://linkingmon.github.io/blog/coding/lc-23/</link><pubDate>Fri, 09 Jan 2026 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-23/</guid><description>&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;This is actually quite simple, just take the smallest from all list and connect, and iteratively do this process, and will use a min heap instead for faster min-val query.&lt;/p&gt;
&lt;p&gt;I also tried a new method for defining the comparator for &lt;code&gt;priority_queue&lt;/code&gt; this time. Usually I will use a comparator class and a &lt;code&gt;bool operator()&lt;/code&gt;, but I found that there is a more clear way to write this, that is to define the lambda function first, and pass the function to pq&amp;rsquo;s constructor.&lt;/p&gt;</description></item><item><title>LC 295 - Find Median from Data Stream</title><link>https://linkingmon.github.io/blog/coding/lc-295/</link><pubDate>Fri, 09 Jan 2026 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-295/</guid><description>&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;I waw originally thinking that we can insert the number with binary search (or lower_bound), and find median with directly taking the number from index for the vector. This strategy can pass the porlbme, but this is actually an &lt;code&gt;O(N)&lt;/code&gt; solution since vector insert can be &lt;code&gt;O(n)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I then find out that we can maintain a minheap and maxheap with &lt;code&gt;O(lgN)&lt;/code&gt; insertion time, and we only need to rebalance two heaps when their size is different by more than 2.&lt;/p&gt;</description></item><item><title>LC 3478 - Choose K Elements With Maximum Sum</title><link>https://linkingmon.github.io/blog/coding/lc-3478/</link><pubDate>Sun, 09 Mar 2025 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-3478/</guid><description>&lt;p&gt;[translate by ChatGPT]
I actually thought of the solution early on, but at first I didn’t notice that the problem requires choosing k elements. I initially thought it only required finding the two largest values.&lt;/p&gt;
&lt;p&gt;Later I realized that I could implement it using a min heap. This also made me feel that I should review how to use a min heap again. The usage of &lt;code&gt;priority_queue&lt;/code&gt; is actually quite simple: just use &lt;code&gt;top()&lt;/code&gt; to get the element, and then &lt;code&gt;push()&lt;/code&gt; / &lt;code&gt;pop()&lt;/code&gt;.&lt;/p&gt;</description></item></channel></rss>