Skip to the code
+50 XP

#545 Dismiss Toasts for Max Points

hard

A game stacks toast notifications in a row, each showing a number in values. You dismiss them one at a time, in any order. Dismissing a toast earns its number times the numbers of its current left and right neighbors. If there is no neighbor on a side, use 1 for that side. After a dismissal, its two neighbors become next to each other.

Write maxDismissPoints(values) that returns the most points you can earn by dismissing every toast.

Example 1

Input: values = [2,4,3]
Output: 33

Dismiss 4 first (2 × 4 × 3 = 24), then 2 (1 × 2 × 3 = 6), then 3 (1 × 3 × 1 = 3).

Example 2

Input: values = [5]
Output: 5

Both neighbors are missing: 1 × 5 × 1.

Constraints

  • 1 <= values.length <= 300
  • 0 <= values[i] <= 100
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.

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