A C# 7-Line DP Solution - 60ms


  • 0
    L
    public int UniquePaths(int m, int n) {
        int[,] path = new int[m, n];
        for(int i = 0; i < path.GetLength(0); i++)
            for(int j = 0; j < path.GetLength(1); j++){
                int up = i == 0 ? 0 : path[i - 1, j], left = j == 0 ? 0 : path[i, j - 1];
                path[i, j] = up + left != 0 ? up + left : 1;
            }
        return path[m - 1, n - 1];
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.