aboutsummaryrefslogtreecommitdiff
path: root/max_unique_subrr_sum_delete.py
diff options
context:
space:
mode:
Diffstat (limited to 'max_unique_subrr_sum_delete.py')
-rw-r--r--max_unique_subrr_sum_delete.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/max_unique_subrr_sum_delete.py b/max_unique_subrr_sum_delete.py
new file mode 100644
index 0000000..0a04acc
--- /dev/null
+++ b/max_unique_subrr_sum_delete.py
@@ -0,0 +1,28 @@
+# 3487. Maximum Unique Subarray Sum After Deletion
+
+"""
+you are given an integer array 'nums'. 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 all elements in the subarray
+are unique, and the sum of all elements in the subarray is maximised. return
+the maximum sum of such a subarray
+"""
+
+
+class Solution(object):
+ def maxSum(self, nums):
+ """
+ :type nums: List[int]
+ :rtype: int
+ """
+ if all(i < 0 for i in nums):
+ return max(nums)
+ unique = set(nums)
+ return sum(i for i in unique if i > 0)
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.maxSum(nums=[1, 2, 3, 4, 5]))
+ print(obj.maxSum(nums=[1, 1, 0, 1, 1]))
+ print(obj.maxSum(nums=[1, 2, -1, -2, 1, 0, -1]))