Backtracking Flashcards
What is backtracking?
It is the most direct way to write code that traverses each possible path
When to know you reached the destination?
row=m, col=n, bottom right corner, and there is one additional unique path to it.
When you should stop traversing?
row>m, col>n, it’s an invalid path
row=r and col=c, what to do?
Two choices: Traverse to the right and traverse to the bottom.
Total unique paths at grid (r,c) is?
it is equal to the sum of total unique paths from the grid to the right and the grid below
Runtime Complexity
Backtracking only allows to explore possibilities, so it is the permutations of a binary string that contains m zeroes and n ones. O(m+n m)
Space Complexity
O(m+n) for the recursion that goes to m+n level