Find the Maximum Number of Fruits Collected - Problem

Welcome to the Fruit Collection Adventure! Imagine three children exploring a magical n × n dungeon filled with delicious fruits. Each room (i, j) contains fruits[i][j] pieces of fruit waiting to be collected.

The three children start at different corners:

  • Child A: Top-left corner (0, 0)
  • Child B: Top-right corner (0, n-1)
  • Child C: Bottom-left corner (n-1, 0)

All three must reach the treasure room at (n-1, n-1) in exactly n-1 moves. Each child has specific movement rules:

  • Child A: Can move to (i+1, j+1), (i+1, j), or (i, j+1) (diagonal, down, or right)
  • Child B: Can move to (i+1, j-1), (i+1, j), or (i+1, j+1) (down-left, down, or down-right)
  • Child C: Can move to (i-1, j+1), (i, j+1), or (i+1, j+1) (up-right, right, or down-right)

Important rule: If multiple children enter the same room, only one gets the fruits! Your goal is to find the maximum total fruits they can collect by choosing optimal paths.

Input & Output

example_1.py — Basic 3x3 Grid
$ Input: fruits = [[1,2,3],[4,5,6],[7,8,9]]
Output: 24
💡 Note: Child A: (0,0)→(1,1)→(2,2) collects 1+5+9=15. Child B: (0,2)→(1,2)→(2,2) would collect 3+6, but (2,2) already taken. Child C: (2,0)→(2,1)→(2,2) would collect 7+8, but (2,2) already taken. Optimal paths give total 24 fruits.
example_2.py — Symmetric Grid
$ Input: fruits = [[1,1,1],[1,1,1],[1,1,1]]
Output: 7
💡 Note: All cells have 1 fruit. Three children must coordinate to visit 7 unique cells total to reach (2,2), collecting 7 fruits maximum.
example_3.py — Edge Case 2x2
$ Input: fruits = [[5,10],[15,20]]
Output: 50
💡 Note: Child A: (0,0)→(1,1) gets 5+20=25. Child B: (0,1)→(1,1) gets 10 (can't get (1,1) again). Child C: (1,0)→(1,1) gets 15 (can't get (1,1) again). Total: 5+10+15+20=50.

Constraints

  • 2 ≤ n ≤ 20
  • 0 ≤ fruits[i][j] ≤ 1000
  • All three children must reach (n-1, n-1) in exactly n-1 moves
  • Movement constraints differ for each child
  • When multiple children occupy same cell, only one collects fruits

Visualization

Tap to expand
Maximum Fruits Collected - 3 Children Problem INPUT n x n Grid (n=3) 1 2 3 4 5 6 7 8 9 A B C Target Child A: (0,0) Child B: (0,n-1) Child C: (n-1,0) fruits = [[1,2,3], [4,5,6],[7,8,9]] ALGORITHM STEPS 1 3D DP Setup dp[i][j1][j2] = max fruits at row i, cols j1, j2 2 Movement Rules A: down, down-right B: down, down-left C: right, down-right 3 Collision Handling If same cell: count once fruits[i][j] only 1 child 4 Maximize Total Try all valid moves Track max at each step Optimal Paths: FINAL RESULT 1 2 3 4 5 6 7 8 9 Collected Fruits: A: 1+4 = 5 B: 3+6 = 9 C: 7+8 = 15 Shared: 9 (counted once) OUTPUT 24 1+3+4+5+6+7+8+9 = 24 OK - Maximum achieved! Key Insight: Use 3D Dynamic Programming where state dp[row][col1][col2] tracks maximum fruits when children B and C are at columns col1 and col2 in the same row. Child A's path is deterministic (diagonal). Handle collisions by counting fruits only once when multiple children visit the same cell. Time: O(n^3), Space: O(n^2). TutorialsPoint - Find the Maximum Number of Fruits Collected | Optimal Solution
Asked in
Google 35 Meta 28 Amazon 22 Microsoft 18
26.4K Views
Medium Frequency
~35 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen