Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Out of Boundary Paths

Posted on June 18, 2019July 26, 2020 by braindenny

Out of Boundary Paths



Similar Problems:

  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #bfs, #dynamicprogramming, #countdistinctmoves

There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However, you can at most move N times. Find out the number of paths to move the ball out of grid boundary. The answer may be very large, return it after mod 109 + 7.

Example 1:

Input: m = 2, n = 2, N = 2, i = 0, j = 0
Output: 6
Explanation:

Out of Boundary Paths

Example 2:

Input: m = 1, n = 3, N = 3, i = 0, j = 1
Output: 12
Explanation:

Out of Boundary Paths

Note:

  1. Once you move the ball out of boundary, you cannot move it back.
  2. The length and height of the grid is in range [1,50].
  3. N is in range [0,50].

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/out-of-boundary-paths
// Basic Ideas: dynamic programming
//
//  Notice: what if starting point is out of the grid?
//
//     optimal substructure: pos[i][j] N 
//            <- pos[i-1][j] N-1, pos[i+1][j] N-1, ....
//      
// Complexity: Time O(m*n*N), Space O(m*n*N)
import "math"
func findPaths(m int, n int, N int, i int, j int) int {
    if N == 0 {
        if i>=0 && i<m && j>=0 && j<n {
            return 0
        } else {
            return 1
        }
    }
    mod := int(math.Pow(10, 9)+7)
    l := make([][]int, m)
    for i, _ := range l {
        l[i] = make([]int, n)
    }

    l[i][j] = 1
    res := 0
    // Termination condition: N==1
    for N>0 {
        l2 := make([][]int, m)
        for index_i, _ := range l2 {
            l2[index_i] = make([]int, n)
        }
        for r, row := range l {
            for c, _ := range row {
                for _, offset := range [][]int{[]int{1, 0}, []int{-1, 0}, 
                                               []int{0, 1}, []int{0, -1}} {
                    i2, j2 := r+offset[0], c+offset[1]
                    if !(i2>=0 && i2<m && j2>=0 && j2<n) {
                        res = (res+l[r][c])%mod
                    } else {
                        l2[i2][j2] = (l2[i2][j2] + l[r][c])%mod
                    }
                }
            }
        }
        copy(l, l2)
        N--
    }
    return res
}
// https://code.dennyzhang.com/out-of-boundary-paths
// Basic Ideas: dynamic programming
//
//  Notice: what if starting point is out of the grid?
//
//     optimal substructure: pos[i][j] N 
//            <- pos[i-1][j] N-1, pos[i+1][j] N-1, ....
//      
// Complexity: Time O(m*n*N), Space O(m*n*N)
import "math"
func findPaths(m int, n int, N int, i int, j int) int {
    if N == 0 {
        if i>=0 && i<m && j>=0 && j<n {
            return 0
        } else {
            return 1
        }
    }
    mod := int(math.Pow(10, 9)+7)
    l := make([][]int, m)
    for i, _ := range l {
        l[i] = make([]int, n)
    }

    // Termination condition: N==1
    for N>0 {
        l2 := make([][]int, m)
        for index_i, _ := range l2 {
            l2[index_i] = make([]int, n)
        }
        for index_i, row := range l {
            for index_j, _ := range row {
                for _, offset := range [][]int{[]int{1, 0}, []int{-1, 0}, 
                                               []int{0, 1}, []int{0, -1}} {
                    i2, j2 := index_i+offset[0], index_j+offset[1]
                    if !(i2>=0 && i2<m && j2>=0 && j2<n) {
                        l2[index_i][index_j] = (l2[index_i][index_j] + 1)%mod
                    } else {
                        l2[index_i][index_j] = (l2[index_i][index_j] + l[i2][j2])%mod
                    }
                }
            }
        }
        copy(l, l2)
        N--
    }
    return l[i][j]
}
// https://code.dennyzhang.com/out-of-boundary-paths
// Basic Ideas: dynamic programming
//
//  Notice: what if starting point is out of the grid?
//
//     optimal substructure: pos[i][j] N 
//            <- pos[i-1][j] N-1, pos[i+1][j] N-1, ....
//      
// Complexity: Time O(m*n*N), Space O(m*n*N)
import "math"
func findPaths(m int, n int, N int, i int, j int) int {
    if N == 0 {
        if i>=0 && i<m && j>=0 && j<n {
            return 0
        } else {
            return 1
        }
    }
    mod := int(math.Pow(10, 9)+7)
    l := make([][]int, m)
    // Termination condition: N==1
    for i, _ := range l {
        l[i] = make([]int, n)
        for j, _ := range l[i] {
            if i == 0 || i == m-1 || j == 0 || j == n-1 {
                for _, offset := range [][]int{[]int{1, 0}, []int{-1, 0},
                                               []int{0, 1}, []int{0, -1}} {
                                        i2, j2 := i+offset[0], j+offset[1]
                                        if !(i2>=0 && i2<m && j2>=0 && j2<n) {
                                                l[i][j]++      
                                        }
                                }
            }
        }
    }
        res := l[i][j]
    for k:=2; k<=N; k++ {
                l2 := make([][]int, m)
                for i, _ := range l2 {
                        l2[i] = make([]int, n)
                }
        for i, _ := range l {
            for j, _ := range l[i] {
                for _, offset := range [][]int{[]int{1, 0}, []int{-1, 0}, 
                                               []int{0, 1}, []int{0, -1}} {
                    i2, j2 := i+offset[0], j+offset[1]
                    if i2>=0 && i2<m && j2>=0 && j2<n {
                        l2[i][j] = (l2[i][j] + l[i2][j2])%mod
                    }
                }
            }
        }
        copy(l, l2)
        res = (res + l[i][j])%mod
    }
    return res
}
linkedin
github
slack

Post Views: 0
Posted in BasicTagged #bfs, #dynamicprogramming, countdistinctmoves

Post navigation

LeetCode: Accounts Merge
LeetCode: Stream of Characters

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.