150強訪談中等

獨特的路徑

“唯一路徑”問題的詳細指南和 Python 實作。

問題陳述

中等

m x n 網格上有一個機器人。機器人最初位於左上角(即 grid[0][0])。機器人嘗試移動到右下角(即 grid[m - 1][n - 1])。機器人在任何時間點只能向下或向右移動。

給定兩個整數 m 和 n,返回機器人到達右下角可能採取的唯一路徑的數量。

寫一個函數 uniquePaths(m: int, n: int) -> int

約束條件
  • 1 <= m, n <= 100

範例

Example 1
Input
m = 3, n = 7
Output
28
Explanation

There are 28 unique paths to go from top-left to bottom-right.

Example 2
Input
m = 3, n = 2
Output
3
Explanation

From the top-left corner, there are 3 ways: Right -> Down -> Down, Down -> Down -> Right, or Down -> Right -> Down.

Need a Hint?
考慮使用 2D DP 特定的資料結構,例如集合或堆疊。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。