diff options
author | Jack Sangdahl <[email protected]> | 2025-07-25 00:17:01 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-07-25 00:17:01 -0600 |
commit | bd21c48fbf4b27bd203b5788b03735f575294df0 (patch) | |
tree | 9444afdae7ffa8d837cfc6b32607427b07f2a9c5 | |
parent | 802fae572423d283c1fe568b2f8e3aba6739f234 (diff) | |
download | leetcode-bd21c48fbf4b27bd203b5788b03735f575294df0.tar.gz leetcode-bd21c48fbf4b27bd203b5788b03735f575294df0.zip |
3487. maximum unique subarray sum after deletion
-rw-r--r-- | max_unique_subrr_sum_delete.c | 40 | ||||
-rw-r--r-- | max_unique_subrr_sum_delete.py | 28 |
2 files changed, 68 insertions, 0 deletions
diff --git a/max_unique_subrr_sum_delete.c b/max_unique_subrr_sum_delete.c new file mode 100644 index 0000000..c4c769e --- /dev/null +++ b/max_unique_subrr_sum_delete.c @@ -0,0 +1,40 @@ +// 3487. Maximum Unique Subarray Sum After Deletion +#include "leetcode.h" + +/* + * 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 + */ + +int maxSum(int *nums, int numsSize) { + bool negative = true; + int max = INT_MIN; + for (int i = 0; i < numsSize; i++) { + if (nums[i] >= 0) + negative = false; + if (nums[i] > max) + max = nums[i]; + } + if (negative) + return max; + bool vis[101] = {false}; + for (int i = 0; i < numsSize; i++) + if (nums[i] >= 0 && nums[i] < 101) + vis[nums[i]] = true; + int sum = 0; + for (int i = 1; i < 101; i++) + if (vis[i]) + sum += i; + return sum; +} + +int main() { + int n1[] = {1, 2, 3, 4, 5}, n2[] = {1, 1, 0, 1, 1}, + n3[] = {1, 2, -1, -2, 1, 0, -1}; + printf("%d\n", maxSum(n1, ARRAY_SIZE(n1))); // expect: 15 + printf("%d\n", maxSum(n2, ARRAY_SIZE(n2))); // expect: 1 + printf("%d\n", maxSum(n3, ARRAY_SIZE(n3))); // expect: 3 +} 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])) |