Problem

Maximum Score With Non-Adjacent Values

Learn this problem
Amazon logoAmazonFULLTIMEONSITE INTERVIEW
See Amazon hiring insights

Problem statement

You are given a list of integers nums. You may choose any set of values from the list.

If you choose a value x, then you cannot choose x - 1 or x + 1. When you choose x, your score increases by x * frequency(x), where frequency(x) is the number of times x appears in nums.

Return the maximum score you can obtain.

Function

maximumNonAdjacentValueScore(nums: int[]) → long

Examples

Example 1

nums = [3,4,2]return = 6

Choose values 2 and 4 for score 2 + 4 = 6. Choosing 3 would block both.

Example 2

nums = [2,2,3,3,3,4]return = 9

Choosing value 3 gives score 3 * 3 = 9, which is better than choosing 2 and 4 for score 8.

Constraints

  • nums.length >= 1

More Amazon problems

drafts saved locally
public long maximumNonAdjacentValueScore(int[] nums) {
  // write your code here
}
nums[3,4,2]
expected6
checking account