#1695

Maximum Erasure Value

specialist · 645 · lc medium +30 · verified · 64.1% accepted · 3,397 likes · top 67%

Description

Given an array of positive integers nums, select a contiguous subarray whose elements are all distinct and whose sum is as large as possible. You delete exactly one such subarray and collect its sum as your score. Return the maximum achievable score.

A subarray b of a is a contiguous slice a[l], a[l+1], ..., a[r] for some valid (l, r).

Example 1:

Input: nums = [4,2,4,5,6]
Output: 17
Explanation: The optimal subarray here is [2,4,5,6].

Example 2:

Input: nums = [5,2,1,2,5,2,1,2,5]
Output: 8
Explanation: The optimal subarray here is [5,2,1] or [1,2,5].

Code

1
2
3