Maximizing Chocolates in Round Trip(Chocolates Pickup II)
Last Updated : 7 Nov, 2025
You are given an n*n grid mat[][], where each cell represents either:
a blocked cell, denoted by -1, or
a cell containing chocolates, denoted by a non-negative integer mat[i][j], representing the number of chocolates in that cell.
A robot starts at the top-left corner (0, 0) and must travel to the bottom-right corner (n-1, n-1) by moving only right or down at each step. After reaching (n-1, n-1), the robot must return to (0, 0), by moving only left or up.
Note: Each time the robot visits a cell, it collects all chocolates from that cell, and the cell becomes empty(mat[i][j] = 0). Thus, chocolates cannot be collected more than once from the same cell.
Determine the maximum number of chocolates the robot can collect during its complete round trip.
Output: 5 Explanation: The maximum number of chocolates in the round trip can be obtained by moving from (0, 0) -> (1, 0) -> (2, 0) -> (2, 1) -> (2, 2), and then returning back to (0, 0) along the path (2, 2) -> (2, 1) -> (1, 1) -> (0, 1) -> (0, 0).
Output: 7 Explanation: The maximum number of chocolates in the round trip can be obtained by moving from (0, 0) -> (1, 0) -> (2, 0) -> (2, 1) -> (2, 2) and then returning back to (0, 0) along the path(2, 2) -> (1, 2) -> (1, 1) -> (0, 1) -> (0, 0).
Input: mat[][] = [[1, 1, -1], [1, -1, 1], [-1, 1, 1]] Output: 0 Explanation: There is no possible path to reach the bottom right (n-1, n-1) cell hence we cannot collect any chocolate.
If the robot moves from (0, 0) to (n-1, n-1) and then back to (0, 0), some cells will be visited twice. To make sure we donβt count the chocolates in those cells twice, we would need to remember which cells were already visited during the first trip. Doing that would require an extra boolean matrix to track visited cells for every possible path, which would take a lot of extra space and make the solution inefficient.
Instead, we can imagine two robots starting from (0, 0) and moving through the grid simultaneously toward (nβ1, nβ1). Both robots can move only right or down at each step, and they collect chocolates from the cells they visit. If both robots happen to visit the same cell, it will occur at the same instant, and the chocolates in that cell are counted only once. This way, we naturally ensure that each cellβs chocolates are added at most once, without needing any extra matrix to track visited cells.
[Naive Approach] - Using Recursion β O(4n) Time and O(n) Space
Both robots start at (0, 0) and move toward (nβ1, nβ1) simultaneously. In each step, both can move either right or down, giving four possible move combinations in total. They collect chocolates from the cells they visit, but if both robots land on the same cell, we count the chocolates there only once. To find the maximum chocolates they can collect together, we explore all possible move combinations at each step and take the one that gives the highest total. This can be solved recursively by trying all four possible moves for both robots and choosing the combination that results in the maximum chocolates.
Output
7
[Better Approach - 1] - Using Memoization β O(n*m2) Time and O(n*m2) Space
In this approach, we notice that many subproblems repeat. For example, to find the maximum chocolates when both robots are at positions (i1, j1) and (i2, j2), we need the results for the next moves β such as (i1+1, j1, i2+1, j2), (i1+1, j1, i2, j2+1), and others. While calculating results for different positions, some of these states are required multiple times. To avoid recomputing the same values, we store the results of already solved states in a DP table and reuse them whenever needed. This helps reduce redundant calculations and makes the solution much more efficient.
As both robots have the same count of total number of moves at any instant, we can calculate the row of the second robot by equating the total number of moves made by both robots. Therefore, we only need 3 states to keep track of the current position of both robots in the grid.
Output
7
[Better Approach - 2] - Using Tabulation β O(n*m2) Time and O(n*m2) Space
In this approach, we iteratively calculate the answers starting from the smallest subproblems β the bottom-right corner of the grid. The answer for any cell, where the two robots are at positions (i1, j1) and (i2, j2), depends on the results of their next possible moves β either moving right or down. Using the precomputed results from the cells below or to the right, we can efficiently calculate the maximum chocolates the robots can collect for the current positions. We store these results in a DP table and continue this process upward and leftward until we reach the starting cell (0, 0).
Here, dp[i1][j1][j2] stores the maximum number of chocolates that both robots can collect when they are at cells (i1, j1) and (i2, j2) (i2 = i1+j1-j2) respectively, and move toward the destination cell (n-1, m-1).
Output
7
[Expected Approach] - Space Optimized DP β O(n*m2) Time and O(m2) Space
In this approach, we iteratively compute the results for one row at a time instead of maintaining the full 3D DP table. We use two 2D arrays β one for the current row and other for the next row. Starting from the bottom-right corner and moving upward, we calculate the maximum chocolates that both robots can collect for each pair of column positions (j1, j2) in row i1. For each state, we use results from the next row when robots move down and from the current row when they move right, since we process the grid from right to left. After finishing a row, we update next = current and move one row up. This way, curr[j1][j2] at an iteration stores the best possible chocolates for positions (i1, j1) and (i2, j2) (i2 = i1 + j1 - j2) using previously computed results, achieving both time and space efficiency.