Problem · Array

Maximize Movie Ratings Without Consecutive Skips

Learn this problem
MediumOracle logoOracleFULLTIMEONSITE INTERVIEW

Problem statement

You are given an integer array ratings, where ratings[i] is the rating of the movie at position i. You may select or skip each movie.

Maximize the sum of the selected ratings subject to this rule: you may not skip two consecutive movies.

For an array containing one movie, you may skip that movie and return 0. For every longer array, your choices must still satisfy the no-two-consecutive-skips rule.

Return the maximum total as a signed 64-bit integer.

Function

maxMovieRatings(ratings: int[]) → long

Examples

Example 1

ratings = [5,-1,4]return = 9

Select ratings 5 and 4 and skip -1. The selected total is 9, and only one movie is skipped.

Example 2

ratings = [-5,-2,-3]return = -2

Skip the first and third movies and select the middle rating -2. The two skipped positions are not adjacent.

Example 3

ratings = [-7]return = 0

A single movie may be skipped, so the best total is 0.

Example 4

ratings = [6,-4,-5,7]return = 9

The ratings -4 and -5 cannot both be skipped. Select 6, -4, and 7 for a maximum total of 9.

Constraints

  • 1 <= ratings.length <= 200000.
  • -2^31 <= ratings[i] <= 2^31 - 1.
  • The returned value fits in a signed 64-bit integer.

More Oracle problems

drafts saved locally
public long maxMovieRatings(int[] ratings) {
  // write your code here
}
ratings[5,-1,4]
expected9
checking account