Skip to the code
cf.
+50 XP

#508 Two Cursors Collecting Stars

hard

A multiplayer canvas game has a grid where grid[r][c] is the number of stars in cell (r, c). Two players' cursors start in the top row: one in the top-left cell and one in the top-right cell. On each turn both cursors move down one row, each to the column on its left, the same column, or the column on its right, staying inside the grid.

Every cell a cursor visits gives its stars. If both cursors are in the same cell, its stars count only once. Write collectStars(grid) that returns the most stars the two players can collect together by the time they reach the bottom row.

Example 1

Input: grid = [[1,0,1],[0,5,0]]
Output: 7

The top corners give 1 + 1. In row 1, one cursor takes the 5; the other can't add more.

Example 2

Input: grid = [[2,3],[4,1]]
Output: 10

2 + 3 on top, then one cursor takes 4 and the other takes 1.

Constraints

  • 1 <= rows <= 50
  • 1 <= cols <= 20
  • 0 <= grid[r][c] <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).