Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created January 27, 2018 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/f6d077483f83b90263868c656450399e to your computer and use it in GitHub Desktop.
Save jianminchen/f6d077483f83b90263868c656450399e to your computer and use it in GitHub Desktop.
Number of path - use only one row - get advised from the peer only to use one row instead of two rows, previous row and current row.
using System;
class Solution
{
public static int NumOfPathsToDest(int n) // n = 4
{
if(n == 0) // false
{
return 0;
}
var current = new int[n]; // 0 to n - 1
for(int row = 0; row < n; row++)
{
for(int col = 0; col < n; col++)
{
if(row > col)
{
current[col] = 0;
}
if(row == 0)
{
current[col] = 1;
}
else if(row < col)
{
current[col] = current[col - 1] + current[col];
}
}
}
return current[n - 1];
}
static void Main(string[] args)
{
}
}
/*
keywords:
go north or go east
go up or go right
cross diagnal
Ask: number of paths to (n - 1, n - 1)
* * * * *
* * * 5 *
* * 2 5 9
0 1 2 3 4
1 1 1 1 1
matrix(3, 3) = 5
Time complexity: (n * n)
Space complexity: O(n)
Do not need previous row - advised by the peer
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment