1695. Maximum Erasure Value
Question
You are given an array of positive integers
nums
and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements.Return the maximum score you can get by erasing exactly one subarray.
An array
b
is called to be a subarray ofa
if it forms a contiguous subsequence ofa
, that is, if it is equal toa[l],a[l+1],...,a[r]
for some(l,r)
.
Solution
滑动窗口+数组统计。
用sum记录窗口内的和,max记录和的最大值。
当新滑入的右侧指针不为0时,从左侧滑出元素并将滑出的元素改为0。
然后将右侧指针指针对应的数组统计改为1并更新当前的和max。
Code
1 | class Solution { |
1695. Maximum Erasure Value
https://xuanhe95.github.io/2022/06/12/1695-Maximum-Erasure-Value/