Tap here to switch tabs
Problem
Submissions

Problem: Find the Maximum Number of Fruits Collected

med
30 min
Try to solve the Find the Maximum Number of Fruits Collected problem.

Statement

You are given an nimesnn imes n integer array fruits where fruits[i][j] represents the number of fruits at cell (i,j)(i, j).

Three children collect fruits in the grid according to the following rules:

  • The first child starts at cell (0,0)(0, 0) and moves to cell (i+1,j+1)(i + 1, j + 1) at each step.

  • The second child starts at cell (0,n−1)(0, n - 1) and can move to cell (i+1,j−1)(i + 1, j - 1), (i+1,j)(i + 1, j), or (i+1,j+1)(i + 1, j + 1) at each step.

  • The third child starts at cell (n−1,0)(n - 1, 0) and can move to cell (i−1,j+1)(i - 1, j + 1), (i,j+1)(i, j + 1), or (i+1,j+1)(i + 1, j + 1) at each step.

Each child makes exactly n−1n - 1 moves and must reach cell (n−1,n−1)(n - 1, n - 1).

When a child visits a cell, they collect all the fruits from that cell. If a cell is visited by more than one child, the fruits in that cell are collected only once.

Return the maximum total number of fruits the three children can collect.

Constraints:

  • 2≤n==2 \leq n == fruits.length ==== fruits[i].length ≤1000\leq 1000

  • 0≤0 \leq fruits[i][j] ≤1000\leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find the Maximum Number of Fruits Collected

med
30 min
Try to solve the Find the Maximum Number of Fruits Collected problem.

Statement

You are given an nimesnn imes n integer array fruits where fruits[i][j] represents the number of fruits at cell (i,j)(i, j).

Three children collect fruits in the grid according to the following rules:

  • The first child starts at cell (0,0)(0, 0) and moves to cell (i+1,j+1)(i + 1, j + 1) at each step.

  • The second child starts at cell (0,n−1)(0, n - 1) and can move to cell (i+1,j−1)(i + 1, j - 1), (i+1,j)(i + 1, j), or (i+1,j+1)(i + 1, j + 1) at each step.

  • The third child starts at cell (n−1,0)(n - 1, 0) and can move to cell (i−1,j+1)(i - 1, j + 1), (i,j+1)(i, j + 1), or (i+1,j+1)(i + 1, j + 1) at each step.

Each child makes exactly n−1n - 1 moves and must reach cell (n−1,n−1)(n - 1, n - 1).

When a child visits a cell, they collect all the fruits from that cell. If a cell is visited by more than one child, the fruits in that cell are collected only once.

Return the maximum total number of fruits the three children can collect.

Constraints:

  • 2≤n==2 \leq n == fruits.length ==== fruits[i].length ≤1000\leq 1000

  • 0≤0 \leq fruits[i][j] ≤1000\leq 1000