You are given a two-dimensional matrix of integers of dimensions N*M, where each cell represents the number of coins in that cell. Alice and Bob have to collect the maximum number of coins. The followings are the conditions to collect coins:
Alice starts from top left corner, i.e., (0, 0) and should reach left bottom corner, i.e., (N-1, 0). Bob starts from top right corner, i.e., (0, M-1) and should reach bottom right corner, i.e., (N-1, M-1).
From a point (i, j), Alice and Bob can move to (i+1, j+1) or (i+1, j-1) or (i+1, j)
They have to collect all the coins that are present at a cell. If Alice has already collected coins of a cell, then Bob gets no coins if goes through that cell again.
For example :
If the matrix is
0 2 4 1
4 8 3 7
2 3 6 2
9 7 8 3
1 5 9 4
Then answer is 47. As, Alice will collect coins 0+8+3+9+1 = 21 coins. Bob will collect coins 1+7+6+8+4 = 26 coins. Total coins is 21+26 = 47 coins.
Input Format:
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of input contains two single space-separated integers 'N' and 'M' representing the number of rows and columns of the matrix respectively.
The next 'N' lines contain 'M' single space-separated integers each representing the coins in a row of the matrix.
Output Format:
The only line of output contains a single integer i.e. The minimum coins that can be collected by Alice and Bob.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 5
1 <= N <= 10^2
1 <= M <= 10^2
0 <= MAT[i][j] <= 10^3
Time Limit: 1 sec
Given a matrix of coins, Alice and Bob have to collect maximum coins with certain conditions.
Alice starts from top left corner and Bob starts from top right corner
They can move to (i+1, j+1) or (i+1, ...read more
Since we want to maximum the value we can get, and we always get the middle value of three selection.
We can first sort piles and take 3 coins [Li, Mi, Hi] in following manners.
n=9
piles = [L1,L2,L3,M2,...read more
Intuition:
The idea is to do both traversals concurrently. We start first from (0, 0) and second traversal from (0, m-1) simultaneously. The important thing to note is, at any part...read more
Intuition:
The idea is to do both traversals concurrently. We start first from (0, 0) and second traversal from (0, m-1) simultaneously. The important thing to note is, at any p...read more
Intuition:
The idea is to do both traversals concurrently. We start first from (0, 0) and second traversal from (0, m-1) simultaneously. The important thing to note is, at any particular st...read more
Intuition:
The idea is to do both traversals concurrently. We start first from (0, 0) and second traversal from (0, m-1) simultaneously. The important thing to note is, at any partic...read more
Top Dunzo Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month