Chocolate Pickup
Ninja has a 'GRID' of size 'R' X 'C'. Each cell of the grid contains some chocolates. Ninja has two friends Alice and Bob, and he wants to collect as many chocolates as possible with the help of his friends.
Initially, Alice is in the top-left position i.e. (0, 0), and Bob is in the top-right place i.e. (0, ‘C’ - 1) in the grid. Each of them can move from their current cell to the cells just below them. When anyone passes from any cell, he will pick all chocolates in it, and then the number of chocolates in that cell will become zero. If both stay in the same cell, only one of them will pick the chocolates in it.
If Alice or Bob is at (i, j) then they can move to (i + 1, j), (i + 1, j - 1) or (i + 1, j + 1). They will always stay inside the ‘GRID’.
Your task is to find the maximum number of chocolates Ninja can collect with the help of his friends by following the above rules.
Example:
Input: ‘R’ = 3, ‘C’ = 4
‘GRID’ = [[2, 3, 1, 2], [3, 4, 2, 2], [5, 6, 3, 5]]
Output: 21
Initially Alice is at the position (0,0) he can follow the path (0,0) -> (1,1) -> (2,1) and will collect 2 + 4 + 6 = 12 chocolates.
Initially Bob is at the position (0, 3) and he can follow the path (0, 3) -> (1,3) -> (2, 3) and will colllect 2 + 2 + 5 = 9 chocolates.
Hence the total number of chocolates collected will be 12 + 9 = 21. there is no other possible way to collect a greater number of chocolates than 21.
Input Format :
The first line of input contains an integer 'T', denoting the number of test cases.
For each test case, the first line contains two integers' R' and 'C' denoting the number of rows and number of columns in the grid.
Next 'R' lines will contain 'C' integers each the value of each cell in the grid.
Output format :
For each test case, print the maximum number of chocolates that can be collected.
Output for each test case will be printed in a separate line.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= ‘T’ <= 10
2 <= 'R', 'C' <= 50
0 <= 'GRID[i][j]'<= 10^2
Time Limit: 1sec
CodingNinjas
author
2y
I was able to pass only 1 test case out of 5
CodingNinjas
author
2y
Brute Force Solution
Approach: Say Alice is at column 'C1' and Bob is at column 'C2' of row 'R'. We will try all possible ways of moving both of them to the next row 'R' + 1 under the given conditions....read more
CodingNinjas
author
2y
Recursive Dynamic Programming Solution
Approach: In the first recursive brute force approach, there are repeated calculations due to overlapping paths. We will use memoization to speed things up.
We wi...read more
CodingNinjas
author
2y
Iterative Dynamic Programming Solution
Approach:
We can also solve this problem using iterative dynamic programming. The approach is similar to the recursive approach with a slight change. We will star...read more
Add answer anonymously...
Top HashedIn by Deloitte Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in HashedIn by Deloitte Software Developer
>
HashedIn by Deloitte Software Developer Interview Questions
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