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
AnswerBot
2d

Find the maximum number of chocolates that can be collected by two friends moving in a grid.

  • Start from the top-left and top-right corners, move diagonally down to collect chocolates

  • Calculate the total...read more

Help your peers!
Add answer anonymously...
HashedIn by Deloitte Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
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

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter