<?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>Math on Yu-Cheng's Notes</title><link>https://linkingmon.github.io/blog/tags/math/</link><description>Recent content in Math on Yu-Cheng's Notes</description><generator>Hugo -- 0.166.0</generator><language>en-us</language><lastBuildDate>Sun, 25 Jan 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://linkingmon.github.io/blog/tags/math/index.xml" rel="self" type="application/rss+xml"/><item><title>LC 1984 - Minimum Difference Between Highest and Lowest of K Scores</title><link>https://linkingmon.github.io/blog/coding/lc-1984/</link><pubDate>Sun, 25 Jan 2026 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-1984/</guid><description>&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;When select &lt;code&gt;k&lt;/code&gt; score that have a minimum difference, we would like to make them as close as possible.&lt;/p&gt;
&lt;p&gt;Therefore, we sort the array and chekc all continuous &lt;code&gt;k&lt;/code&gt; elements max difference (that is the &lt;code&gt;i&lt;/code&gt; and &lt;code&gt;i+k-1&lt;/code&gt; element&amp;rsquo;s difference after sorting), ans find the minimum among them.&lt;/p&gt;
&lt;h2 id="code"&gt;Code&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Solution&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;public&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; minimumDifference(vector&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&amp;amp;&lt;/span&gt; nums, &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; k) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; sort(nums.begin(), nums.end());
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n &lt;span style="color:#f92672"&gt;=&lt;/span&gt; nums.size();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; minVal &lt;span style="color:#f92672"&gt;=&lt;/span&gt; nums[n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;] &lt;span style="color:#f92672"&gt;-&lt;/span&gt; nums[&lt;span style="color:#ae81ff"&gt;0&lt;/span&gt;];
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt; ; i &lt;span style="color:#f92672"&gt;&amp;lt;=&lt;/span&gt; n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;k ; &lt;span style="color:#f92672"&gt;++&lt;/span&gt;i) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; minVal &lt;span style="color:#f92672"&gt;=&lt;/span&gt; min(minVal, nums[i&lt;span style="color:#f92672"&gt;+&lt;/span&gt;k&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;] &lt;span style="color:#f92672"&gt;-&lt;/span&gt; nums[i]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; minVal;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>LC 1877 - Minimize Maximum Pair Sum in Array</title><link>https://linkingmon.github.io/blog/coding/lc-1877/</link><pubDate>Sat, 24 Jan 2026 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-1877/</guid><description>&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;This seems to be a difficult problem, but it is acutally simple.&lt;/p&gt;
&lt;p&gt;When we select pairs for &lt;code&gt;a &amp;lt;= b &amp;lt;= c &amp;lt;= d&lt;/code&gt;, it is obvious that selecting &lt;code&gt;(a,d)&lt;/code&gt; with &lt;code&gt;(b,c)&lt;/code&gt; can achieve the minimax; therefore, we only need to sort the original array and find the max with two pointers from head and tali.&lt;/p&gt;
&lt;h2 id="code"&gt;Code&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c++" data-lang="c++"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;class&lt;/span&gt; &lt;span style="color:#a6e22e"&gt;Solution&lt;/span&gt; {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#66d9ef"&gt;public&lt;/span&gt;&lt;span style="color:#f92672"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; minPairSum(vector&lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt;&lt;span style="color:#f92672"&gt;&amp;gt;&amp;amp;&lt;/span&gt; nums) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; sort(nums.begin(), nums.end());
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; sum &lt;span style="color:#f92672"&gt;=&lt;/span&gt; INT_MIN;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; n &lt;span style="color:#f92672"&gt;=&lt;/span&gt; nums.size();
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;for&lt;/span&gt;(&lt;span style="color:#66d9ef"&gt;int&lt;/span&gt; i &lt;span style="color:#f92672"&gt;=&lt;/span&gt; &lt;span style="color:#ae81ff"&gt;0&lt;/span&gt; ; i &lt;span style="color:#f92672"&gt;&amp;lt;&lt;/span&gt; n&lt;span style="color:#f92672"&gt;/&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;2&lt;/span&gt; ; &lt;span style="color:#f92672"&gt;++&lt;/span&gt;i) {
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; sum &lt;span style="color:#f92672"&gt;=&lt;/span&gt; max(sum, nums[i] &lt;span style="color:#f92672"&gt;+&lt;/span&gt; nums[n&lt;span style="color:#f92672"&gt;-&lt;/span&gt;&lt;span style="color:#ae81ff"&gt;1&lt;/span&gt;&lt;span style="color:#f92672"&gt;-&lt;/span&gt;i]);
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#66d9ef"&gt;return&lt;/span&gt; sum;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;};
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>LC 3314 - Construct the Minimum Bitwise Array I</title><link>https://linkingmon.github.io/blog/coding/lc-3314/</link><pubDate>Tue, 20 Jan 2026 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-3314/</guid><description>&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;I found that the answer for this is find the longest continuous 1&amp;rsquo;s in the number, and remove that 1. For instance, &lt;code&gt;1000111 -&amp;gt; 1000011&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I also found an interesting thing to do this without using while loop to shift, the formula for this is &lt;code&gt;n &amp;amp; ~((~n &amp;amp; (n+1)) &amp;gt;&amp;gt; 1)&lt;/code&gt;. The part &lt;code&gt;(~n &amp;amp; (n+1)) &amp;gt;&amp;gt; 1&lt;/code&gt; is the highest continuos 1 bit since for &lt;code&gt;n = ? 0 1 .. 1&lt;/code&gt;, we have &lt;code&gt;n+1 = ? 1 0 .. 0&lt;/code&gt;, then &lt;code&gt;~n &amp;amp; (n+1) = 0 1 0 .. 0&lt;/code&gt;, shifting this by one is the highest bit.&lt;/p&gt;</description></item><item><title>LC 1975 - Maximum Matrix Sum</title><link>https://linkingmon.github.io/blog/coding/lc-1975/</link><pubDate>Mon, 05 Jan 2026 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-1975/</guid><description>&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;This is a math like problem, I think that if a list is given we can always swap the first one to positive and propagate the operation.&lt;/p&gt;
&lt;p&gt;In the end, there might be no negative number or just a negative number in the end. This depends on the total number of negative numbers in the original case, since the operatoin would not change the odd/even for the total negative count, if original matrix is having odd count, the last element would be negative.&lt;/p&gt;</description></item><item><title>LC 1390 - Four Divisors</title><link>https://linkingmon.github.io/blog/coding/lc-1390/</link><pubDate>Sun, 04 Jan 2026 00:00:00 +0000</pubDate><guid>https://linkingmon.github.io/blog/coding/lc-1390/</guid><description>&lt;h2 id="approach"&gt;Approach&lt;/h2&gt;
&lt;p&gt;Originally I thought that we just need to find the number that can be divided by two primes. &lt;code&gt;num = prime1 * prime2&lt;/code&gt;, but got an WA, since I&amp;rsquo;ve ignored the &lt;code&gt;num = prime1 * prime1 * prime1&lt;/code&gt;, such as 8 has 1, 2, 4, 8 as divisors.&lt;/p&gt;
&lt;p&gt;So the overall method is that we precalculate the prime and check the above conditions.&lt;/p&gt;
&lt;p&gt;My first submit is slow since I tried to build prime up to maxNum, but actually we can build to &lt;code&gt;floor(sqrt(maxNum))&lt;/code&gt;. I was thinking that when &lt;code&gt;prime1&lt;/code&gt; is smaller in this square-root range, but &lt;code&gt;prime2&lt;/code&gt; would not, so I build the entire prime inside the whole range. Actually, even when &lt;code&gt;prime2&lt;/code&gt; is larger then the square-root range, we can walk the prime again and check if there are other divisors (this can reduce lots of complexity!).&lt;/p&gt;</description></item></channel></rss>