<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>老博客 on Code Now</title><link>https://blog.0xnullpath.cc/en/tags/%E8%80%81%E5%8D%9A%E5%AE%A2/</link><description>Recent content in 老博客 on Code Now</description><generator>Hugo</generator><language>en</language><lastBuildDate>Tue, 14 Apr 2020 17:57:00 +0000</lastBuildDate><atom:link href="https://blog.0xnullpath.cc/en/tags/%E8%80%81%E5%8D%9A%E5%AE%A2/index.xml" rel="self" type="application/rss+xml"/><item><title>EM Algorithm Notes</title><link>https://blog.0xnullpath.cc/en/posts/note-snippet-16-em-algorithm-notes/</link><pubDate>Tue, 14 Apr 2020 17:57:00 +0000</pubDate><guid>https://blog.0xnullpath.cc/en/posts/note-snippet-16-em-algorithm-notes/</guid><description>&lt;blockquote>
 &lt;p>I recently ran into the EM algorithm again. Too often I just use something without knowing why, so this time, before using it, I went back and read up on it some more.&lt;/p>
&lt;/blockquote>&lt;p>Where the EM algorithm applies: maximum likelihood estimation of the parameters of a probabilistic model that has hidden variables.&lt;/p>
&lt;p>First question: what is a hidden variable?&lt;/p>
&lt;h2 id="latent-variables">Latent Variables&lt;/h2>
&lt;blockquote>
 &lt;p>Something that cannot be directly observed, but that influences the state of the system and the outputs that can be observed. It refers to an unobservable random variable. Latent variables can be inferred from observed data by using a mathematical model.&lt;/p></description></item><item><title>Python Basics: Decorators</title><link>https://blog.0xnullpath.cc/en/posts/note-snippet-17-python-basics-decorators/</link><pubDate>Fri, 20 Mar 2020 16:54:00 +0000</pubDate><guid>https://blog.0xnullpath.cc/en/posts/note-snippet-17-python-basics-decorators/</guid><description>&lt;p>A decorator is a callable object whose argument is another function (the decorated function). The decorator may process the decorated function and then return it, or replace it with another function or callable object.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#a6e22e">@test&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span>&lt;span style="color:#66d9ef">def&lt;/span> &lt;span style="color:#a6e22e">target&lt;/span>():
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span> print(&lt;span style="color:#e6db74">&amp;#34;run target function!&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>It works the same as the following code&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-python" data-lang="python">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">1&lt;/span>&lt;span>&lt;span style="color:#66d9ef">def&lt;/span> &lt;span style="color:#a6e22e">target&lt;/span>():
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">2&lt;/span>&lt;span> print(&lt;span style="color:#e6db74">&amp;#34;run target function!&amp;#34;&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">3&lt;/span>&lt;span>target &lt;span style="color:#f92672">=&lt;/span> test(target) &lt;span style="color:#75715e"># In the test function, the function will be enhanced or replaced&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Function decorators run at import time, while the decorated function only runs when it is explicitly called.&lt;/p></description></item><item><title>[Python Basics] Namespaces, Scope, and Closures</title><link>https://blog.0xnullpath.cc/en/posts/note-snippet-18-python-basics-namespaces-scope-and-closures/</link><pubDate>Fri, 20 Mar 2020 16:53:00 +0000</pubDate><guid>https://blog.0xnullpath.cc/en/posts/note-snippet-18-python-basics-namespaces-scope-and-closures/</guid><description>&lt;blockquote>
 &lt;p>A namespace is a &lt;strong>mapping&lt;/strong> from names to objects. Most namespaces are currently implemented as Python dictionaries, but that&amp;rsquo;s normally not noticeable in any way (except for performance), and it may change in the future.&lt;/p>
&lt;/blockquote>&lt;p>Namespaces are an important structure for avoiding name collisions. Namespaces are dynamic: they come into being as the interpreter executes, and they are maintained throughout the life of the program.&lt;/p>
&lt;p>Namespaces:&lt;/p>
&lt;ul>
&lt;li>Built-in namespace: the namespace that comes with the Python interpreter as soon as it starts, and stops when the Python interpreter ends.&lt;/li>
&lt;li>Global namespace: the set of global names of a Python module — names defined directly in the module, such as classes, functions, other imported modules, and so on. Destroyed when the interpreter exits.&lt;/li>
&lt;li>Local namespace: the namespace inside a function or a class. It is created when a function is called, and deleted when the function returns or raises an error that is not handled inside the function.&lt;/li>
&lt;li>The attribute namespace of an object&lt;/li>
&lt;li>Class namespace&lt;/li>
&lt;/ul>
&lt;h1 id="scope">Scope&lt;/h1>
&lt;p>A &lt;em>scope&lt;/em> is a &lt;strong>textual region&lt;/strong> of a Python program where a namespace is directly accessible. &amp;ldquo;Directly accessible&amp;rdquo; here means that an unqualified reference to a name attempts to find the name in the namespace.&lt;/p></description></item><item><title>Inversions in an array</title><link>https://blog.0xnullpath.cc/en/posts/note-snippet-19-inversions-in-an-array/</link><pubDate>Tue, 10 Mar 2020 18:11:00 +0000</pubDate><guid>https://blog.0xnullpath.cc/en/posts/note-snippet-19-inversions-in-an-array/</guid><description>&lt;blockquote>
 &lt;p>Interview Question 51. Inversions in an Array
Given two numbers in an array, if the earlier number is greater than the later one, the two numbers form an inversion. Given an array, find the total number of inversions in it.&lt;/p>
&lt;/blockquote>&lt;p>Example 1:&lt;/p>
&lt;pre tabindex="0">&lt;code>Input: [7,5,6,4]
Output: 5
&lt;/code>&lt;/pre>&lt;p>Constraints:
0 &amp;lt;= array length &amp;lt;= 50000&lt;/p>
&lt;h1 id="approach">Approach&lt;/h1>
&lt;h2 id="brute-force-solution">Brute-force solution&lt;/h2>
&lt;p>Just scan it mindlessly&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-cpp" data-lang="cpp">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1&lt;/span>&lt;span>&lt;span style="color:#66d9ef">class&lt;/span> &lt;span style="color:#a6e22e">Solution&lt;/span> {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2&lt;/span>&lt;span>&lt;span style="color:#66d9ef">public&lt;/span>&lt;span style="color:#f92672">:&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3&lt;/span>&lt;span> &lt;span style="color:#66d9ef">int&lt;/span> reversePairs(vector&lt;span style="color:#f92672">&amp;lt;&lt;/span>&lt;span style="color:#66d9ef">int&lt;/span>&lt;span style="color:#f92672">&amp;gt;&amp;amp;&lt;/span> nums) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4&lt;/span>&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> (nums.size() &lt;span style="color:#f92672">==&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5&lt;/span>&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6&lt;/span>&lt;span> &lt;span style="color:#66d9ef">int&lt;/span> sum &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7&lt;/span>&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> (&lt;span style="color:#66d9ef">int&lt;/span> i &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span>; i &lt;span style="color:#f92672">&amp;lt;&lt;/span> nums.size() &lt;span style="color:#f92672">-&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span>; &lt;span style="color:#f92672">++&lt;/span>i) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8&lt;/span>&lt;span> &lt;span style="color:#66d9ef">for&lt;/span> (&lt;span style="color:#66d9ef">int&lt;/span> j &lt;span style="color:#f92672">=&lt;/span> i&lt;span style="color:#f92672">+&lt;/span>&lt;span style="color:#ae81ff">1&lt;/span>; j &lt;span style="color:#f92672">&amp;lt;&lt;/span> nums.size(); &lt;span style="color:#f92672">++&lt;/span>j) {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9&lt;/span>&lt;span> &lt;span style="color:#66d9ef">if&lt;/span> (nums[i] &lt;span style="color:#f92672">&amp;gt;&lt;/span> nums[j])
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10&lt;/span>&lt;span> sum&lt;span style="color:#f92672">++&lt;/span>;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11&lt;/span>&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12&lt;/span>&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13&lt;/span>&lt;span> &lt;span style="color:#66d9ef">return&lt;/span> sum;
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14&lt;/span>&lt;span> }
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">15&lt;/span>&lt;span>};
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Time complexity $O(n^2)$, space complexity $O(1)$&lt;/p></description></item><item><title>Fenwick Tree (Binary Indexed)</title><link>https://blog.0xnullpath.cc/en/posts/note-snippet-12-fenwick-tree-binary-indexed/</link><pubDate>Tue, 10 Mar 2020 17:30:00 +0000</pubDate><guid>https://blog.0xnullpath.cc/en/posts/note-snippet-12-fenwick-tree-binary-indexed/</guid><description>&lt;blockquote>
 &lt;p>First, many thanks to the Bilibili creator 鹤翔万里 for the video; I recommend watching &lt;a href="https://www.bilibili.com/video/av69667943?from=search&amp;amp;seid=10916758362943551299">https://www.bilibili.com/video/av69667943?from=search&amp;seid=10916758362943551299&lt;/a>. This article is a summary I wrote for the 算法笔记 collection accompanying that video.&lt;/p>
&lt;/blockquote>&lt;p>Problem setup: given an array of length &lt;code>n&lt;/code>, support the following two operations&lt;/p>
&lt;ol>
&lt;li>Output the sum of every number in the interval &lt;code>[x, y]&lt;/code>&lt;/li>
&lt;li>Add &lt;code>k&lt;/code> to the &lt;code>x&lt;/code>-th number&lt;/li>
&lt;/ol>
&lt;p>The most basic algorithm:&lt;/p>
&lt;ol>
&lt;li>Maintain an array &lt;code>sum&lt;/code>, where &lt;code>sum[i]&lt;/code> stores the sum from 0 to i. Its recurrence relation is &lt;code>sum[i] = sum[i-1] + nums[i]&lt;/code>, where &lt;code>sum[0] = nums[0]&lt;/code>.&lt;/li>
&lt;li>The interval sum can then be computed using &lt;code>sum_xy = sum[y] - sum[x-1]&lt;/code>.&lt;/li>
&lt;li>To add &lt;code>v&lt;/code> to the &lt;code>x&lt;/code>-th number, since all values of &lt;code>sum[x]...sum[n]&lt;/code> must be updated, the time complexity is $O(n)$&lt;/li>
&lt;li>If &lt;code>k&lt;/code> operations are performed, &lt;code>add&lt;/code>, then the time complexity is $O(kn)$, and the time complexity of &lt;code>k&lt;/code> interval-sum queries is $O(k)$&lt;/li>
&lt;/ol>
&lt;p>In many scenarios with frequent updates, the $O(kn)$ time complexity is unacceptable.&lt;/p></description></item></channel></rss>