Apple Pickup
Alice always loves to visit her garden and collect apples. The garden can be represented in the form of ‘N’ * ’N’ grid say ‘MATRIX’, where each cell of the grid can have one of the possible values:
1 -> The cell contains an apple that Alice can pick up and pass through it.
-1 -> The cell contains a bush and Alice can not visit this cell.
0 -> The cell is empty and Alice can pass through it.
Alice is present at the top left corner of the matrix or we can say at point (0,0).
Alice has to reach the bottom right corner of the matrix (‘N’-1,’N’-1) and return back to the starting point (0,0).
1. After picking an apple the cell will become empty.
2. While going towards the bottom right corner, Alice can either move Right or Down at each step.
3. While going towards the top left corner, Alice can either move Left or Up at each step.
4. If there is no path from (0,0) to (‘N’-1, ‘N’-1) then Alice will not pick any apple.
Your task is to help Alice to collect the maximum number of apples during her journey.
For example:
If the given matrix is :
[1, 1, -1, 1]
[1, 0, 1, 1]
[1, 1, 0, 1]
[0, -1, -1, 1]
One of the possible ways to collect maximum apples is :
Path for going towards bottom right corner:
(0,0) -> (0,1) -> (1,1) -> (1,2) -> (1,3) -> (2,3) -> (3,3)
Apples collected are equal to 6.
Path for going towards top left corner:
(3,3) -> (2,3) ->(2,2) -> (2,1) -> (2,0) -> (1,0) -> (0,0)
Apples collected are equal to 3.
So Alice can collect a maximum of 9 apples.
Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases. Then each test case follows.
The first line of each test case contains a space-separated integer ‘N’ representing the number of rows and columns.
The next ‘N’ lines of each test case contain ‘N’ single space-separated integers denoting the values of ‘MATRIX’.
Output format:
For each test case, print a single line containing a single integer representing the maximum number of apples Alice can collect.
The output of every test case will be printed in a separate line.
Note :
You don’t have to print anything, it has already been taken care of. Just implement the given function.
Constraints
1 <= T <=10
1 <= N <= 50
-1 <= MATRIX[i] <= 1
Where ‘T’ denotes the number of test cases, ‘N’ denotes the number of rows and columns of ‘MATRIX’.
Time limit: 1 sec.
CodingNinjas
author
2y
I solved this using dynamic programming.
CodingNinjas
author
2y
Recursion
The idea is to explore two paths simultaneously from (0,0) to (‘N’-1, ‘N’-1) and collect maximum apples. However, while implementing this approach we may count 1 apple two times, so we need ...read more
CodingNinjas
author
2y
Recursion + Memoization
Our last approach was very simple and easy, but its time complexity was of exponential order. We can improve our solution by taking care of the overlapping subproblems. Thus, we...read more
CodingNinjas
author
2y
Bottom-UP DP
As our earlier approach recursion with memoization surely avoided some subproblems but we can still improve time complexity using a bottom-up approach and we will also reduce the space co...read more
Add answer anonymously...
Top Cisco Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Cisco Software Developer
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app