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 中测试过 · 没有依赖
Each page imports the same modules that the tests check; nothing is rewritten for the web.
每个页面引入的都是测试检查过的那些模块,没有为网页另写一份。
Twelve sorts replay every read and write they make. Race two on the same numbers and see which needs fewer steps.
十二种排序回放自己做的每一次读和写。让两种排序在同一组数上比赛,看哪一种需要的步数更少。
Fifteen LeetCode problems, several approaches each, timed on the same input in your browser: brute force against the optimal solution.
十五道 LeetCode 题,每道有几种解法,在你的浏览器里用同一份输入计时:暴力解对最优解。
Two hand-written segmenters and your browser's own read a Chinese sentence; a graph shows why greedy matching misreads 和尚.
两个手写的分词器和你的浏览器自带的分词器读同一个中文句子;一张图说明贪心匹配为什么会错把“和尚”当成一个词。
Every file starts with a comment that explains the idea and its cost, and runs an example with node.
每个文件开头都有一段注释,讲清思路和代价;用 node 运行它,就会打印一个示例。
Fourteen sorts from bubble to radix, several with a Python twin, checked against the built-in sort and raced by a bench.
从冒泡到基数的十四种排序,部分有 Python 版,都和内置排序对照检查过,还有一个让它们比赛的计时脚本。
Binary search and lower bound in JavaScript and Java, and KMP string search.
JavaScript 和 Java 的二分查找与 lower bound,以及 KMP 字符串查找。
Stacks, queues, a binary heap, linked lists, a set, a hash table, trees, a trie and a graph.
栈、队列、二叉堆、链表、集合、哈希表、树、前缀树和图。
Adding large numbers, removing duplicates three ways, the 0-1 knapsack, the Dutch national flag and more.
大数相加、三种去重方法、0-1 背包、荷兰国旗,等等。
Sixty problems, one file per approach, each folder with a test that checks every approach against the others.
六十道题,每种解法一个文件;每个目录都有一个测试,让所有解法互相对照。
Forward maximum matching and jieba's graph with dynamic programming, next to Intl.Segmenter.
正向最大匹配,以及 jieba 的词典建图加动态规划,与 Intl.Segmenter 对照。
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 |