Find the Maximum Number of Fruits Collected
expert · 1085 · lc hard +32 · 65.1% accepted · 450 likes · top 69%
Description
A game dungeon consists of n x n rooms arranged in a grid.
You are given a 2D array fruits of size n x n, where fruits[i][j] is the number of fruits in room (i, j). Three children start at corner rooms (0, 0), (0, n - 1), and (n - 1, 0), each making exactly n - 1 moves to reach (n - 1, n - 1):
- The child from (0, 0) moves from (i, j) to one of (i + 1, j + 1), (i + 1, j), or (i, j + 1).
- The child from (0, n - 1) moves from (i, j) to one of (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1).
- The child from (n - 1, 0) moves from (i, j) to one of (i - 1, j + 1), (i, j + 1), or (i + 1, j + 1).
When a child enters a room, all fruits are collected. If multiple children reach the same room, only one collects the fruits.
Return the maximum number of fruits the children can collectively gather.
Code