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
ABC๐Ÿดโ€โ˜ ๏ธ Treasure Hunt Coordination๐ŸŽฏ Goal: Maximize total fruits collectedโšก Strategy: 3D DP with memoization๐Ÿง  Key Insight: Track all positions simultaneously๐ŸŽฏ Target: (n-1, n-1)๐Ÿ‘ค Child A: Diagonal movement๐Ÿ‘ค Child B: Downward movement๐Ÿ‘ค Child C: Rightward movement
Understanding the Visualization
1
Setup
Three children start at corners: A at (0,0), B at (0,n-1), C at (n-1,0)
2
Movement Rules
Each child has different movement constraints based on their starting position
3
Conflict Handling
When children occupy the same cell, only one can collect the fruits
4
Dynamic Programming
Use 3D DP to track all positions and memoize optimal solutions
5
Optimal Solution
Return maximum fruits collected through coordinated optimal paths
Key Takeaway
๐ŸŽฏ Key Insight: Use 3D Dynamic Programming to simultaneously track all three children's positions, handling conflicts optimally through memoized state transitions.
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