|
| 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