Skip to content

Commit eb4cf1c

Browse files
committed
Add solution #3487
1 parent 00dc9df commit eb4cf1c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,6 +2709,7 @@
27092709
3466|[Maximum Coin Collection](./solutions/3466-maximum-coin-collection.js)|Medium|
27102710
3476|[Maximize Profit from Task Assignment](./solutions/3476-maximize-profit-from-task-assignment.js)|Medium|
27112711
3481|[Apply Substitutions](./solutions/3481-apply-substitutions.js)|Medium|
2712+
3487|[Maximum Unique Subarray Sum After Deletion](./solutions/3487-maximum-unique-subarray-sum-after-deletion.js)|Easy|
27122713
3491|[Phone Number Prefix](./solutions/3491-phone-number-prefix.js)|Easy|
27132714
3496|[Maximize Score After Pair Deletions](./solutions/3496-maximize-score-after-pair-deletions.js)|Medium|
27142715

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 3487. Maximum Unique Subarray Sum After Deletion
3+
* https://leetcode.com/problems/maximum-unique-subarray-sum-after-deletion/
4+
* Difficulty: Easy
5+
*
6+
* You are given an integer array nums.
7+
*
8+
* You are allowed to delete any number of elements from nums without making it empty.
9+
* After performing the deletions, select a subarray of nums such that:
10+
* 1. All elements in the subarray are unique.
11+
* 2. The sum of the elements in the subarray is maximized.
12+
*
13+
* Return the maximum sum of such a subarray.
14+
*/
15+
16+
/**
17+
* @param {number[]} nums
18+
* @return {number}
19+
*/
20+
var maxSum = function(nums) {
21+
const uniqueValues = [...new Set(nums)];
22+
const positiveValues = uniqueValues.filter(val => val > 0);
23+
24+
if (positiveValues.length === 0) {
25+
return Math.max(...uniqueValues);
26+
}
27+
28+
return positiveValues.reduce((sum, val) => sum + val, 0);
29+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy