Ctrl K

Three Gatherers in the Orchard

Hard
An orchard is an n × n grid, and cell (i, j) holds orchard[i][j] fruits. Three gatherers start at the corners (0, 0), (0, n - 1) and (n - 1, 0). Each makes exactly n - 1 moves and must finish at (n - 1, n - 1). - The one from (0, 0) moves from (i, j) to (i + 1, j + 1), (i + 1, j) or (i, j + 1). - The one from (0, n - 1) moves to (i + 1, j - 1), (i + 1, j) or (i + 1, j + 1). - The one from (n - 1, 0) moves to (i - 1, j + 1), (i, j + 1) or (i + 1, j + 1). Nobody may leave the orchard. A gatherer takes all the fruit from every cell it visits, the starting cell included. When several gatherers visit the same cell, its fruit is counted only once. Return the largest number of fruits the three can collect together.
Example 1
orchard=[[2, 0, 5], [1, 3, 4], [6, 7, 1]] → 28

The first gatherer walks the diagonal: 2 + 3 + 1. The second goes (0, 2) → (1, 2) → (2, 2) and collects 5 + 4. The third goes (2, 0) → (2, 1) → (2, 2) and collects 6 + 7. The corner (2, 2) is already empty. Total 6 + 9 + 13 = 28.

Example 2
orchard=[[5, 0], [0, 5]] → 10

Each gatherer makes one step into the bottom-right corner; both 5s are collected, 10 in total.

Constraints
  • 2 ≤ n ≤ 1000
  • len(orchard) = len(orchard[i]) = n
  • 0 ≤ orchard[i][j] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBiggest Solid Panel Block HardCrossing the Cave HardLongest Uphill Walk HardWeeks of Rest by Flight HardBerry Field Round Trip Hard
loading Python…
Input
orchard=[[2, 0, 5], [1, 3, 4], [6, 7, 1]]
Expected
28

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.