Algorithm
本周选择的算法题是:Unique Binary Search Trees。
规则
Given an integer n
, return the number of structurally unique BST’s (binary search trees) which has exactly n
nodes of unique values from 1
to n
.
Example 1:
Input: n = 3
Output: 5
Example 2:
Input: n = 1
Output: 1
Constraints:
1 <= n <= 19
Solution
class Solution:
def numTrees(self, n: int) -> int:
G = [0] * (n+1)
G[0] = G[1] = 1
for i in range(2, n+1):
for j in range(1, i+1):
G[i] += G[j-1] * G[i-j]
return G[-1]
Review
How Single Page Applications Broke Web Design
作者在文中提到的状态管理问题并不是 SPA 引入的,只要代码设计的不合理 - 更准确的说程序员的能力太差,都会造成状态混乱,而 SPA 实际上并没有加重这一问题,恰恰相反,状态管理甚至变简单了;SEO、首页加载慢确实是存在的,但都有对应的解决方案,如 SSR、5G 等。
Tip
开始尝试用 Brave 作为日常浏览器。
Python3 中抑制异常的方法:
import contextlib
with contextlib.suppress(FileNotFoundError):
os.remove('somefile.tmp')
# This is equivalent to:
try:
os.remove('somefile.tmp')
except FileNotFoundError:
pass