Skip to content

Commit 7f75ad3

Browse files
committed
feat: using cache instead of lru_cache in python solutions
cache and lru_cache(maxsize=None) are exactly the same (linkto cpython source). If we don't want to limit the cache size, using cache may make the code clearer, since a least recently used cache without limit doesn't make much sense.
1 parent 273f37f commit 7f75ad3

File tree

38 files changed

+38
-49
lines changed

38 files changed

+38
-49
lines changed

lcof/面试题43. 1~n整数中1出现的次数/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ high=3,lows=356,base=1000。此时 n 可拆分为 `0~999`,`1000~1999`,`2000~2999
5959
<!-- 这里可写当前语言的特殊实现逻辑 -->
6060

6161
```python
62-
from functools import lru_cache
63-
6462
class Solution:
65-
@lru_cache
63+
@cache
6664
def countDigitOne(self, n: int) -> int:
6765
if n < 1:
6866
return 0

lcof/面试题43. 1~n整数中1出现的次数/Solution.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
from functools import lru_cache
2-
3-
41
class Solution:
5-
@lru_cache
2+
@cache
63
def countDigitOne(self, n: int) -> int:
74
if n < 1:
85
return 0

lcof2/剑指 Offer II 096. 字符串交织/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Solution:
7575
if m + n != len(s3):
7676
return False
7777

78-
@lru_cache
78+
@cache
7979
def dfs(i, j):
8080
if i == m and j == n:
8181
return True

lcof2/剑指 Offer II 096. 字符串交织/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
44
if m + n != len(s3):
55
return False
66

7-
@lru_cache
7+
@cache
88
def dfs(i, j):
99
if i == m and j == n:
1010
return True

lcof2/剑指 Offer II 101. 分割等和子串/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class Solution:
105105
return False
106106
target = s >> 1
107107

108-
@lru_cache(None)
108+
@cache
109109
def dfs(i, s):
110110
nonlocal target
111111
if s > target or i >= len(nums):

lcof2/剑指 Offer II 102. 加减的目标值/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class Solution:
125125
```python
126126
class Solution:
127127
def findTargetSumWays(self, nums: List[int], target: int) -> int:
128-
@lru_cache(None)
128+
@cache
129129
def dfs(i, t):
130130
if i == n:
131131
if t == target:

lcof2/剑指 Offer II 112. 最长递增路径/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
```python
6767
class Solution:
6868
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
69-
@lru_cache(None)
69+
@cache
7070
def dfs(i, j):
7171
ans = 1
7272
for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

lcof2/剑指 Offer II 112. 最长递增路径/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
3-
@lru_cache(None)
3+
@cache
44
def dfs(i, j):
55
ans = 1
66
for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

solution/0000-0099/0097.Interleaving String/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Solution:
7575
if m + n != len(s3):
7676
return False
7777

78-
@lru_cache
78+
@cache
7979
def dfs(i, j):
8080
if i == m and j == n:
8181
return True

solution/0000-0099/0097.Interleaving String/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Solution:
6464
if m + n != len(s3):
6565
return False
6666

67-
@lru_cache
67+
@cache
6868
def dfs(i, j):
6969
if i == m and j == n:
7070
return True

solution/0000-0099/0097.Interleaving String/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def isInterleave(self, s1: str, s2: str, s3: str) -> bool:
44
if m + n != len(s3):
55
return False
66

7-
@lru_cache
7+
@cache
88
def dfs(i, j):
99
if i == m and j == n:
1010
return True

solution/0300-0399/0329.Longest Increasing Path in a Matrix/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
```python
6161
class Solution:
6262
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
63-
@lru_cache(None)
63+
@cache
6464
def dfs(i, j):
6565
ans = 1
6666
for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

solution/0300-0399/0329.Longest Increasing Path in a Matrix/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
```python
5252
class Solution:
5353
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
54-
@lru_cache(None)
54+
@cache
5555
def dfs(i, j):
5656
ans = 1
5757
for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

solution/0300-0399/0329.Longest Increasing Path in a Matrix/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def longestIncreasingPath(self, matrix: List[List[int]]) -> int:
3-
@lru_cache(None)
3+
@cache
44
def dfs(i, j):
55
ans = 1
66
for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:

solution/0300-0399/0337.House Robber III/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
# self.right = right
6666
class Solution:
6767
def rob(self, root: TreeNode) -> int:
68-
@lru_cache(None)
68+
@cache
6969
def dfs(root):
7070
if root is None:
7171
return 0

solution/0300-0399/0337.House Robber III/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
# self.right = right
5151
class Solution:
5252
def rob(self, root: TreeNode) -> int:
53-
@lru_cache(None)
53+
@cache
5454
def dfs(root):
5555
if root is None:
5656
return 0

solution/0300-0399/0337.House Robber III/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# self.right = right
77
class Solution:
88
def rob(self, root: TreeNode) -> int:
9-
@lru_cache(None)
9+
@cache
1010
def dfs(root):
1111
if root is None:
1212
return 0

solution/0400-0499/0416.Partition Equal Subset Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Solution:
9898
return False
9999
target = s >> 1
100100

101-
@lru_cache(None)
101+
@cache
102102
def dfs(i, s):
103103
nonlocal target
104104
if s > target or i >= len(nums):

solution/0400-0499/0416.Partition Equal Subset Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Solution:
7979
return False
8080
target = s >> 1
8181

82-
@lru_cache(None)
82+
@cache
8383
def dfs(i, s):
8484
nonlocal target
8585
if s > target or i >= len(nums):

solution/0400-0499/0464.Can I Win/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
```python
6868
class Solution:
6969
def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
70-
@lru_cache(None)
70+
@cache
7171
def dfs(state, t):
7272
for i in range(1, maxChoosableInteger + 1):
7373
if (state >> i) & 1:

solution/0400-0499/0464.Can I Win/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Same with other integers chosen by the first player, the second player will alwa
5757
```python
5858
class Solution:
5959
def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
60-
@lru_cache(None)
60+
@cache
6161
def dfs(state, t):
6262
for i in range(1, maxChoosableInteger + 1):
6363
if (state >> i) & 1:

solution/0400-0499/0464.Can I Win/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def canIWin(self, maxChoosableInteger: int, desiredTotal: int) -> bool:
3-
@lru_cache(None)
3+
@cache
44
def dfs(state, t):
55
for i in range(1, maxChoosableInteger + 1):
66
if (state >> i) & 1:

solution/0400-0499/0494.Target Sum/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ DFS:
106106
```python
107107
class Solution:
108108
def findTargetSumWays(self, nums: List[int], target: int) -> int:
109-
@lru_cache(None)
109+
@cache
110110
def dfs(i, t):
111111
if i == n:
112112
if t == target:

solution/0400-0499/0494.Target Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ DFS:
9292
```python
9393
class Solution:
9494
def findTargetSumWays(self, nums: List[int], target: int) -> int:
95-
@lru_cache(None)
95+
@cache
9696
def dfs(i, t):
9797
if i == n:
9898
if t == target:

solution/0700-0799/0787.Cheapest Flights Within K Stops/README.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,9 @@ class Solution:
8282
```
8383

8484
```python
85-
from functools import lru_cache
86-
87-
8885
class Solution:
8986
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
90-
@lru_cache(None)
87+
@cache
9188
def dfs(u, k):
9289
if u == dst:
9390
return 0

solution/0700-0799/0787.Cheapest Flights Within K Stops/README_EN.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,9 @@ class Solution:
7575
```
7676

7777
```python
78-
from functools import lru_cache
79-
80-
8178
class Solution:
8279
def findCheapestPrice(self, n: int, flights: List[List[int]], src: int, dst: int, k: int) -> int:
83-
@lru_cache(None)
80+
@cache
8481
def dfs(u, k):
8582
if u == dst:
8683
return 0

solution/0900-0999/0913.Cat and Mouse/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
```python
7474
class Solution:
7575
def catMouseGame(self, graph: List[List[int]]) -> int:
76-
@lru_cache(None)
76+
@cache
7777
def dfs(i, j, k):
7878
# 老鼠 / 猫 / 总步数
7979
if k >= 2 * len(graph):

solution/0900-0999/0913.Cat and Mouse/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
```python
6767
class Solution:
6868
def catMouseGame(self, graph: List[List[int]]) -> int:
69-
@lru_cache(None)
69+
@cache
7070
def dfs(i, j, k):
7171
# mouse / cat / steps
7272
if k >= 2 * len(graph):

solution/0900-0999/0913.Cat and Mouse/Solutioin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def catMouseGame(self, graph: List[List[int]]) -> int:
3-
@lru_cache(None)
3+
@cache
44
def dfs(i, j, k):
55
# mouse / cat / steps
66
if k >= 2 * len(graph):

solution/1400-1499/1462.Course Schedule IV/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ DFS 记忆化搜索。
8080
```python
8181
class Solution:
8282
def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]:
83-
@lru_cache(None)
83+
@cache
8484
def dfs(a, b):
8585
if b in g[a] or a == b:
8686
return True

solution/1400-1499/1462.Course Schedule IV/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ DFS.
6868
```python
6969
class Solution:
7070
def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]:
71-
@lru_cache(None)
71+
@cache
7272
def dfs(a, b):
7373
if b in g[a] or a == b:
7474
return True

solution/1400-1499/1462.Course Schedule IV/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Solution:
22
def checkIfPrerequisite(
33
self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]
44
) -> List[bool]:
5-
@lru_cache(None)
5+
@cache
66
def dfs(a, b):
77
if b in g[a] or a == b:
88
return True

solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
```python
8585
class Solution:
8686
def minimumCost(self, sentence: str, k: int) -> int:
87-
@lru_cache(None)
87+
@cache
8888
def dfs(i):
8989
if s[-1] - s[i] + n - i - 1 <= k:
9090
return 0

solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The cost of the last row is not included in the total cost, and since there is o
7575
```python
7676
class Solution:
7777
def minimumCost(self, sentence: str, k: int) -> int:
78-
@lru_cache(None)
78+
@cache
7979
def dfs(i):
8080
if s[-1] - s[i] + n - i - 1 <= k:
8181
return 0

solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def minimumCost(self, sentence: str, k: int) -> int:
3-
@lru_cache(None)
3+
@cache
44
def dfs(i):
55
if s[-1] - s[i] + n - i - 1 <= k:
66
return 0

solution/2200-2299/2267.Check if There Is a Valid Parentheses String Path/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
```python
7777
class Solution:
7878
def hasValidPath(self, grid: List[List[str]]) -> bool:
79-
@lru_cache(None)
79+
@cache
8080
def dfs(i, j, t):
8181
if grid[i][j] == '(':
8282
t += 1

solution/2200-2299/2267.Check if There Is a Valid Parentheses String Path/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Note that there may be other valid parentheses string paths.
6262
```python
6363
class Solution:
6464
def hasValidPath(self, grid: List[List[str]]) -> bool:
65-
@lru_cache(None)
65+
@cache
6666
def dfs(i, j, t):
6767
if grid[i][j] == '(':
6868
t += 1

solution/2200-2299/2267.Check if There Is a Valid Parentheses String Path/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def hasValidPath(self, grid: List[List[str]]) -> bool:
3-
@lru_cache(None)
3+
@cache
44
def dfs(i, j, t):
55
if grid[i][j] == '(':
66
t += 1

0 commit comments

Comments
 (0)