Unique Paths

来源:互联网 发布:装修网络平台有哪些 编辑:程序博客网 时间:2024/05/23 11:41

题目

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?

分析

这是一道动态规划题,状态转移方程为a[i][j] = a[i-1][j] + a[i][j-1]

算法复杂度为O(mn),空间复杂度为O(m)。

代码

class Solution {public:    int uniquePaths(int m, int n) {        int *a = new int[m];        int i,j;        for (i = 0;i < m;i++)        a[i] = 1;        for (i = 1;i < n;i++)        for (j = 1;j < m;j++)        a[j] += a[j-1];        return a[m-1];    }};


0 0
原创粉丝点击