C# solution: 1D dp


  • 0
    B
    public class Solution 
    {
        public int UniquePaths(int m, int n) 
        {
            var preLeft = 1;
    
            var preTops = new int[n];
    
            for (int i = 0; i < n; i++)
            {
                preTops[i] = 1;
            }
    
            for (int i = 1; i < m; i++)
            {
                for (int j = 1; j < n; j++)
                {
                    preTops[j] = preTops[j] + preLeft;
                    preLeft = preTops[j];
                }
    
                preLeft = 1;
            }
    
            return preTops[n - 1];
        }
    }
    

Log in to reply
 

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