Simple scala solution


  • 0
    K

    Create an Array which stores the possible number paths for each block, if it is zero then calculate the possible ways otherwise return possible ways from the cache.

    object Solution {
        def uniquePaths(m: Int, n: Int): Int = {
            var cach: Array[Int] = new Array(m * n)
            cach.update(0, 1)
            
            def noOfPathsAt(p: Int, q: Int): Int = {
                if (cach(p * n + q) == 0) {
                    var count: Int = 0
                    if (p > 0)
                        count += noOfPathsAt(p-1, q)
                    if (q > 0)
                        count += noOfPathsAt(p, q-1)
                    cach.update(p * n + q, count)
                    return count
                } else {
                    return cach(p * n + q)
                }
            }
            noOfPathsAt(m-1, n-1)
        }
    }
    

Log in to reply
 

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