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.
Examples
01 · Example 1
nums = [3,4,2] return = 6
Choose values 2 and 4 for score 2 + 4 = 6. Choosing 3 would block both.
02 · 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
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Maximum Non-Adjacent House ValueONSITE INTERVIEW · Seen Jun 2026
- Running Delivery Time MediansONSITE INTERVIEW · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
public long maximumNonAdjacentValueScore(int[] nums) {
// write your code here
}nums[3,4,2]
expected6
sign in to submit