Maximum points by traversing from top left of Matrix to bottom right by two persons
Given a matrix grid of order N*M with numbers 0-9 in the cells. The task is to find the maximum amount of money collected when two persons move from (0, 0) to (N-1, M-1) by moving only right and down. If both persons are at the same cell, then only one of them can pick the money at that location.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
1 1 1
1 0 1
1 1 1
Explanation: Let 1 denote the places where person 1 collects the money and 2 denote where person 2 does so, then a possible solution is
1 1 1
2 0 1
2 2 1
0 9 9 3 3
2 9 3 3 3
0 3 3 3 3
4 1 1 1 1
Approach: The problem can be solved by using the recursion, by moving both the persons down and right in each of the cell and finding the maximum path sum in all the paths from (0, 0) to (N-1, M-1). So the idea is to find the cost of all possible paths and to find the maximum of them.
Below is the implementation of the above approach:
Time Complexity: O(2N*2M)
Auxiliary Space: O((N*M)2)