Han writes stuffs hereAlgorithms, Math, Rants
https://hanyou.dev
A Visualized Distributed Algorithm Playground
Play and experiment with your favorite distributed algorithms! Everything visualized.
No script link: dist.hanyou.dev
Sun, 08 Sep 2019 04:32:00 -0600
https://hanyou.dev/2019-09-08-dist-demo/
https://hanyou.dev/2019-09-08-dist-demo/Type of `flip id`
What is the type of flip id? \[ flip :: (a \rightarrow b \rightarrow c) \rightarrow (b \rightarrow a \rightarrow c) \\ id :: x \rightarrow x \] Because id is the parameter of flip, we can solve \[ (a \rightarrow (b \rightarrow c)) \sim (x \rightarrow x) \] to...
Sun, 25 Aug 2019 01:14:00 -0600
https://hanyou.dev/2019-08-25-flip-id/
https://hanyou.dev/2019-08-25-flip-id/Amortized LRU Cache (Now WITHOUT Linked List!)
As you probably know LRU cache is a common interview question. Just for the record, I know that the canonical way of doing this is using a hashmap with a linked list, but to implement a generic linked list correctly is somewhat unpleasant when there are special cases such as...
Mon, 03 Jun 2019 11:51:00 -0600
https://hanyou.dev/2019-06-03-amortized-lru/
https://hanyou.dev/2019-06-03-amortized-lru/Covering a Boolean Matrix with Minimum Number of Rectangles
The problem (by @rubyleehs): Suppose we have a boolean matrix. How do we use the fewest (maybe overlapping) rectangles to cover all the true cells and none of the false cells? Given a $n\times n$ matrix, obviously the lowerbound of this problem is $O(n^2)$. So that’s what we are aiming...
Mon, 08 Apr 2019 06:27:30 -0600
https://hanyou.dev/2019-04-08-minimum-maxtrix-cover/
https://hanyou.dev/2019-04-08-minimum-maxtrix-cover/Nested Chairman Tree (Functional Segment Tree), and Beyond
Recently I ran into one (of the millions of variations of) sequence maintainence problem (BZOJ3065). We need to support insertion/deletion, and querying the $k$th smallest element in a given interval. Hzwer seems to have come up with a solution that involves a scapegoat-tree-nested segment tree, which I can’t say confidently...
Wed, 03 Apr 2019 16:27:06 -0600
https://hanyou.dev/2019-04-03-nested-functional-segment-tree/
https://hanyou.dev/2019-04-03-nested-functional-segment-tree/Notes on GHC (1) - The Core Language
Introduction I have been working on ES.hs for a month or two. ES.hs is a compiler that compiles Haskell to ES6 code. There are already a few options out there: there is GHCJS, which takes over after the LLVM phase. This renders communication with Javascript not very straightforward but in...
Mon, 04 Mar 2019 09:49:59 -0700
https://hanyou.dev/2019-03-04-ghc-notes-1/
https://hanyou.dev/2019-03-04-ghc-notes-1/What does Haskell RankNTypes do? (1) - Semantics
Consider this function: tuple2Int :: (x -> Int) -> (a, b) -> (Int, Int) tuple2Int f (a, b) = (f a, f b) …which wouldn’t work. Why? Because of f a, the type checker deduces that x~a (x is equivalent to a); because of f b, the type checker also...
Tue, 26 Feb 2019 15:33:26 -0700
https://hanyou.dev/2019-02-26-haskell-rankntype-1/
https://hanyou.dev/2019-02-26-haskell-rankntype-1/Be Careful of Direct-Indexing and Modifying C++ Containers Simultaneously
There was this weird C++ bug I encountered, minimally reproducible by #include <iostream> #include <vector> std::vector<int> vec; int alloc(){ vec.push_back(0); return vec.size(); } int main(){ vec.push_back(0); vec[0] = alloc(); std::cout << vec[0] << std::endl; } After the first line of main, vec has size 1. In the second line of...
Tue, 26 Feb 2019 14:19:27 -0700
https://hanyou.dev/2019-02-26-cpp-alloc-access/
https://hanyou.dev/2019-02-26-cpp-alloc-access/Yet Another Method to Generate Permutations
There are tons of methods to generate every permutation of some elements. And here’s another intuitive and efficient (beats 98.00% of submissions on LeetCode) one I came up with. Suppose we have $N$ elements $a_1, a_2 … a_N$ and $answer$ is a 1-index (yeah, burn me) list sized $n!$ (so...
Wed, 06 Feb 2019 04:58:54 -0700
https://hanyou.dev/2019-02-06-yet-another-combination-gen/
https://hanyou.dev/2019-02-06-yet-another-combination-gen/