小黑代码:排列组合推导法
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
# C(m+n-2)^(m-1) or C(m+n-2)^(n-1)
# 求组合数分母
top_res = 1
for i in range(n, m + n - 1):
top_res *= i
bottom_res = 1
for i in range(1, m):
bottom_res *= i
return top_res // bottom_res
小黑代码:记忆化搜索
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
map_ = {}
def dfs(x = 0, y = 0):
if (x, y) in map_:
return map_[(x, y)]
# 出界
if x > m - 1 or y > n - 1:
return 0
# 到达终点
if x == m - 1 and y == n - 1:
return 1
right = dfs(x, y + 1)
bottom = dfs(x + 1, y)
map_[(x, y)] = right + bottom
return map_[(x, y)]
return dfs()
动态规划
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
# 初始化动态规划数组
dp = [[0] * n for _ in range(m)]
for i in range(m):
dp[i][0] = 1
for j in range(n):
dp[0][j] = 1
# 开始进行动态规划推导
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i-1][j] + dp[i][j-1]
return dp[m-1][n-1]
数学法
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
top = m + n - 2
bottom = m - 1
top_ans = 1
bottom_ans = 1
for i in range(m-1):
top_ans *= top
bottom_ans *= bottom
top -= 1
bottom -= 1
return int(top_ans // bottom_ans)
小黑生活
最后一晚的实习
看到大家论文还没怎么些呢,
晚上练完琴奖励自己的夜宵
小汤3终于到货啦
活力早餐,准备南二环LSD
开启南二环之旅,2023小黑第一跑&&两杯喜茶
好久不见,约个饭~大姐姐帮助小黑解答情感困惑哈哈哈哈
回学校找老黑们喝酒去啦
好久不回宿舍,却没有之前在宿舍喝酒那么开心了,马上毕业了,都在为未来而焦虑,真想回到2年前,甚至6年前
戴一戴中老黑的戒指
当年小黑的糗事
佛系小老黑
忧愁中老黑
很早就睡了,睡的真舒服,起来直接回家了
坐个公交回家,又碰到堵车了。。。