A robot is located at the top-left corner of a m x n grid, marked “Start” in the, diagram below).
The robot can only move either down or rightat any one moment. The robot is trying to reach the bottom-right corner of the grid, marked “Finish” in the diagram below.
How many possible unique paths are there?
Above is a 7 x 3 grid. How many possible unique paths are there?
Like our previous problem, we immediately know that we should use the bottom-up approach to solve this since we’re required to find all possible unique paths.
In any bottom-up approach, we need to first establish some base cases. I think of base cases as cases that cannot be broken down any further — they’re the simplest form of a sub-problem. If I can compute them in one step, independent of any previous results, then it must be a base case. Usually a good start to finding a base case is to look at the problem’s smallest input size (i.e., an edge case). In our case, this would be m=1 and n=1. So how many unique paths if m=1 and n=1. It would just be 1.
OK — we have established a base case. Are there any more?
Remember that a robot can _only move down or right at any point in time. _That meansthat if m=1, the value of n doesn’t matter — there’s only one unique path to get there. Similarly, if n=1, the value of m doesn’t matter — again, there’s only one unique path.
Wow! This base case is even more insightful than the first one we established, which is merely a subset of this base case!
Moral of the story: Look for as many base cases as you can before trying to work your way up
#python #computer-science #interview-questions #programming #interview