🤖המדריך להייטיקיסט המתחיל
בינוניLeetCode #62

Unique Paths

עשויה להישאל ב:Microsoft

תיאור השאלה

There is a robot on an m x n grid. The robot is initially located at the top-left corner. The robot tries to move to the bottom-right corner. How many possible unique paths are there?

איך פותרים?

Use a 1D dynamic programming array to accumulate paths column by column.

פתרון קוד

JavaScript
Python
function uniquePaths(m, n) {
  const dp = new Array(n).fill(1);
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      dp[j] += dp[j - 1];
    }
  }
  return dp[n - 1];
}

הסבר וידאו כיצד לפתור את השאלה

Book Cover

לעבור את ראיון העבודה הבא שלך בהצלחה

קורס דיגיטלי מקיף עם +25 שיעורים מעשיים, כשעתיים של וידאו, וליווי של מראיין בכיר.

התחל עכשיו ב-149 ₪ בלבד! 🚀