Espresso Algorithm

Try different algorithms for the same problem, from brute force to optimal. Pure concentration, like an espresso.

同一道题,试试不同的算法:从暴力解一路走到最优解。纯粹浓缩,像一杯 espresso。

Every algorithm here is a small file you can read in a few minutes, checked by tests against its neighbours. Watch the sorts move, race brute force against the optimal solution in your own browser, and see how a program finds the words in Chinese.

这里的每个算法都是一个几分钟就能读完的小文件,测试拿它和旁边的解法互相对照。看排序一步步进行,在你自己的浏览器里让暴力解和最优解比赛,再看看程序怎样在中文里找出词来。

Watch the sorts看排序如何进行Race the solutions让解法比赛View on GitHub在 GitHub 上查看

60 LeetCode problems · 14 sorts, 12 of them animated · every page tested in Chrome, Firefox and Safari · no dependencies

60 道 LeetCode 题 · 14 种排序,其中 12 种有动画 · 每个页面都在 Chrome、Firefox 和 Safari 中测试过 · 没有依赖

crema油脂one shot一份浓缩O(1)

Try it in your browser在浏览器里试试

Each page imports the same modules that the tests check; nothing is rewritten for the web.

每个页面引入的都是测试检查过的那些模块,没有为网页另写一份。

Read the code读代码

Every file starts with a comment that explains the idea and its cost, and runs an example with node.

每个文件开头都有一段注释,讲清思路和代价;用 node 运行它,就会打印一个示例。

From brute force to optimal从暴力解到最优解

Ten problems whose folders hold a whole sequence of approaches, each faster than the one before. Race them on your own device to see the gaps.

这十道题的目录里各有一整串解法,一个比一个快。在你自己的设备上让它们比赛,看看差距有多大。

Problem题目The steps一步步变快Race比赛
509. Fibonacci Number509. 斐波那契数O(φⁿ)→O(n)→O(n), O(1) space→O(log n)O(φⁿ)→O(n)→O(n),空间 O(1)→O(log n)Race比赛
322. Coin Change322. 零钱兑换exponential→O(amount · k) with memoization→the same as a table指数级→记忆化,O(amount · k)→表格法,复杂度相同Race比赛
1143. Longest Common Subsequence1143. 最长公共子序列O(2^(m + n))→O(m · n) with memoization→the same as a tableO(2^(m + n))→记忆化,O(m · n)→表格法,复杂度相同Race比赛
256. Paint House256. 粉刷房子O(2ⁿ)→O(n) with memoization→the same as a tableO(2ⁿ)→记忆化,O(n)→表格法,复杂度相同Race比赛
121. Best Time to Buy and Sell Stock121. 买卖股票的最佳时机O(n²)→O(n) table→O(n), O(1) spaceO(n²)→表格法,O(n)→O(n),空间 O(1)Race比赛
53. Maximum Subarray53. 最大子数组和O(n²)→O(n) table→O(n), O(1) space (Kadane)O(n²)→表格法,O(n)→O(n),空间 O(1)(Kadane 算法)Race比赛
5. Longest Palindromic Substring5. 最长回文子串O(n²) table→O(n²), but O(1) space and far fewer steps表格法,O(n²)→仍是 O(n²),但空间 O(1),步数少得多Race比赛
752. Open the Lock752. 打开转盘锁breadth-first search→from both ends, meeting in the middle广度优先搜索→从两端同时搜索,在中间相遇Race比赛
26. Remove Duplicates from Sorted Array26. 删除有序数组中的重复项O(n²) with splice→O(n) with two pointers用 splice,O(n²)→双指针,O(n)Race比赛
215. Kth Largest Element in an Array215. 数组中的第K个最大元素O(n log n) sort→O(n) quickselect, in Python排序,O(n log n)→快速选择,O(n),Python 版Python