Leetcode 62: Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

 

 1 public class Solution {
 2     public int UniquePaths(int m, int n) {
 3         var dp = new int[m, n];
 4         
 5         for (int i = m - 1; i >= 0; i--)
 6         {
 7             for (int j = n - 1; j >= 0; j--)
 8             {
 9                 if (i == m - 1 || j == n - 1)
10                 {
11                     dp[i, j] = 1;
12                 }
13                 else
14                 {
15                     dp[i, j] = dp[i + 1, j] + dp[i, j + 1];
16                 }
17             }
18         }
19         
20         return dp[0, 0];
21     }
22 }

 

posted @ 2017-11-10 05:09  逸朵  阅读(134)  评论(0)    收藏  举报