Skip to the code
cf.
+25 XP

#496 Featured Slides on a Looping Carousel

medium

A homepage carousel loops: after the last slide it shows the first one again, so the first and last slides are neighbors. values[i] is the value of featuring slide i. Two neighboring slides may not both be featured.

Write featureSlides(values) that returns the largest total value of featured slides. Featuring none is allowed.

Example 1

Input: values = [4,5,4]
Output: 5

Slides 0 and 2 are neighbors in the loop, so the best is slide 1 alone.

Example 2

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

Slides 0 and 2: 2 + 4.

Constraints

  • 1 <= values.length <= 1000
  • 0 <= values[i] <= 1000
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).