[Java] Permutation approach O(min(m,n))


#1

The number of right steps that the robot need to take (rS) = #Columns - 1 = n - 1;
The number of down steps that the robot need to take (dS) = #Rows - 1 = m - 1;

Thus, total number of ways in which the right and down steps can be taken = permutation of all possible steps = (rS + dS)! / (rS! * dS!)

public class Solution {
    public int uniquePaths(int A, int B) {
        int rS = B - 1;
        int dS = A - 1;
        long n = 1;
        int l =  Math.max(rS, dS);
        for (int i = (rS + dS); i > l; i--) {
            n *= i;
        }
        
        long d = Math.max(1, Math.min(rS, dS));
        for (int i = (int)d - 1; i > 1; i--) {
            d *= i;
        }
        
        return (int)(n / d);
    }
}