Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0992e7d

Browse files
authored
Create Maximum Unique Subarray Sum After Deletion.py
1 parent c1befb1 commit 0992e7d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
You are given an integer array nums.
3+
4+
You are allowed to delete any number of elements from nums without making it empty. After performing the deletions, select a subarray of nums such that:
5+
6+
All elements in the subarray are unique.
7+
The sum of the elements in the subarray is maximized.
8+
Return the maximum sum of such a subarray.
9+
'''
10+
11+
---------------------------
12+
#my own solution:
13+
class Solution:
14+
def maxSum(self, nums: List[int]) -> int:
15+
16+
17+
negs = [x for x in nums if x <0]
18+
19+
if len(negs) == len(nums):
20+
return max(negs)
21+
if len(nums) == 1:
22+
return sum(nums)
23+
24+
n = len(nums)
25+
26+
27+
nums = list(set(nums))
28+
29+
res = sum([x for x in nums if x > 0])
30+
return res
31+
32+
---------------------------------------------------------------

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /