Chocolate Pickup Problem
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 their help.
Alice starts at the top-left corner (0, 0), and Bob starts at the top-right corner (0, C-1). From their current positions, they can move to the cells directly below, to their left-diagonal below, or their right-diagonal below. Chocolates collected from any cell will become zero, and if both stop at the same cell, only one will collect the chocolates.
Your task is to find the maximum number of chocolates that can be collected by following the movement rules.
Input:
The first line contains an integer 'T', the number of test cases.
For each test case:
- The first line contains two integers 'R' and 'C'.
- The next 'R' lines each contain 'C' integers, representing the grid.
Output:
Output the maximum number of chocolates that can be collected for each test case, on a new line.
Example:
Input:
'R' = 3, 'C' = 4
'GRID' = [[2, 3, 1, 2], [3, 4, 2, 2], [5, 6, 3, 5]]
Output:
21
Explanation:
Alice follows the path (0,0) -> (1,1) -> (2,1) and collects 2 + 4 + 6 = 12 chocolates.
Bob follows the path (0,3) -> (1,3) -> (2,3) and collects 2 + 2 + 5 = 9 chocolates.
The total chocolates collected is 21.
Constraints:
- 1 <= 'T' <= 10
- 2 <= 'R', 'C' <= 50
- 0 <= 'GRID[i][j]' <= 102
- Time Limit: 1 sec
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
Reviews
Interviews
Salaries
Users/Month