Open xxleyi opened 5 years ago
暴露出来的薄弱环节主要是二维数组使用比较生疏,不能信手拈来。
其实手写带缓存版本的逻辑基本是一致且冗余的,所以完全可以使用装饰器来一次性解决这个问题。更进一步的,Python 中其实已经提供了一个更好的装饰器:functools.lru_cache
:
lru_cache?
Signature: lru_cache(maxsize=128, typed=False)
Docstring:
Least-recently-used cache decorator.
If *maxsize* is set to None, the LRU features are disabled and the cache
can grow without bound.
If *typed* is True, arguments of different types will be cached separately.
For example, f(3.0) and f(3) will be treated as distinct calls with
distinct results.
Arguments to the cached function must be hashable.
View the cache statistics named tuple (hits, misses, maxsize, currsize)
with f.cache_info(). Clear the cache and statistics with f.cache_clear().
Access the underlying function with f.__wrapped__.
See: http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
File: /usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/functools.py
Type: function
# 优化之后的带缓存递归版本
import functools
cache = functools.lru_cache(maxsize=None)
def get_max_lcs_len(m, n):
@cache
def _get_max(i, j):
if i == 0 or j == 0:
return 0
if m[i - 1] == n[j - 1]:
return 1 + _get_max(i - 1, j - 1)
return max(_get_max(i - 1, j), _get_max(i, j - 1))
return _get_max(len(m), len(n))
怎么样,是不是超级酷?Python 中写递归还是蛮舒服的。
⚠️是 subsequence, 而不是 substring
这个问题之前研究过,但那时候是对着答案理解梳理的。本次独立进行一遍全部过程,遇到问题就克服,然后记下来,作为自己的薄弱环节。
这个问题是典型的动态规划,之前我自己总结过一个解决动态规划问题的基本过程:
怎么样,是不是出奇的不可思议的一致。