The fastest possible algorithm -- Java


  • 0
    D
    public class Solution {
        public int uniquePaths(int m, int n) {
            int loop = m-1;
            if (n < m) loop = n-1;
            
            int start = m+n-2;
            int cut = loop;
            double answer = 1;
            
            for (int i = 0; i < loop; i++)
               answer *= ((double)(start--))/(cut--);
           return (int)(answer + 0.5);
        }
    }

Log in to reply
 

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