All problems
0063MediumArrayDynamic Programming

Best Multiplier Tile Run

Tracked in this browser only
Write code

Trains the technique from

LeetCode 152Maximum Product Subarray

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A board game deals out a row of scoring tiles. Tile i carries the whole number factors[i], which may be negative, zero or positive.

On your turn you pick one unbroken stretch of neighbouring tiles, hold at least one tile, and walk across the whole stretch. You enter with a score of 1 and your score is multiplied by each tile's number as you step onto it. Skipping a tile inside the stretch is not allowed.

Report the highest score any single stretch can leave you with.

Examples

Example 1

Input
factors = [3, -1, -4, 2]
Output
24

Walking all four tiles pairs the two negative numbers off against each other, giving 3 * -1 * -4 * 2 = 24. Stopping earlier never gets past 12.

Example 2

Input
factors = [-7, 0, -5]
Output
0

Both single negative tiles leave a negative score, so the best available stretch is one that steps on the zero tile.

Example 3

Input
factors = [-6]
Output
-6

A stretch has to hold at least one tile, so the only choice available scores -6.

Constraints

  • 1 <= factors.length <= 2 * 10^4
  • -10 <= factors[i] <= 10
  • The score of any unbroken stretch of factors is guaranteed to fit in a signed 32-bit integer.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def best_tile_run(factors: list[int]) -> int:
Java
public int bestTileRun(int[] factors)
September 7
Apply