From 2ee92040f33dc1ba24e04df955136852ca1fd395 Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Fri, 2 Dec 2022 09:18:59 +0400 Subject: [PATCH] feat: add Maximum Erasure Value LeetCode problem (#1137) * add leetcode Maximum Erasure Value * Update 1695.c add new line at the end * Rename README.md to DIRECTORY.md * chore: apply suggestions from code review * Update DIRECTORY.md * Update leetcode/DIRECTORY.md Co-authored-by: Taj Co-authored-by: David Leal Co-authored-by: Taj --- leetcode/DIRECTORY.md | 1 + leetcode/src/1695.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 leetcode/src/1695.c diff --git a/leetcode/DIRECTORY.md b/leetcode/DIRECTORY.md index 9c1e26af..3b7fd16b 100644 --- a/leetcode/DIRECTORY.md +++ b/leetcode/DIRECTORY.md @@ -101,6 +101,7 @@ | 1184 | [Distance Between Bus Stops](https://leetcode.com/problems/distance-between-bus-stops/) | [C](./src/1184.c) | Easy | | 1189 | [Maximum Number of Balloons](https://leetcode.com/problems/maximum-number-of-balloons/) | [C](./src/1189.c) | Easy | | 1207 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [C](./src/1207.c) | Easy | +| 1695 | [Maximum Erasure Value](https://leetcode.com/problems/maximum-erasure-value/) | [C](./src/1695.c) | Medium | | 1769 | [Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [C](./src/1769.c) | Medium | | 1524 | [Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [C](./src/1524.c) | Medium | | 1653 | [Minimum Deletions to Make String Balanced](https://leetcode.com/problems/minimum-deletions-to-make-string-balanced/) | [C](./src/1653.c) | Medium | diff --git a/leetcode/src/1695.c b/leetcode/src/1695.c new file mode 100644 index 00000000..c0a57247 --- /dev/null +++ b/leetcode/src/1695.c @@ -0,0 +1,29 @@ +// Window sliding. Runtime: O(n), Space: O(n) +int maximumUniqueSubarray(int* nums, int numsSize){ + short* numsSet = (short*)calloc(10001, sizeof(short)); + numsSet[nums[0]] = 1; + + int maxSum = nums[0]; + + int windowSumm = maxSum; + int leftIndex = 0; + + int num = 0; + for(int i = 1; i < numsSize; i++){ + num = nums[i]; + while (numsSet[num] != 0){ + numsSet[nums[leftIndex]] = 0; + windowSumm -= nums[leftIndex]; + leftIndex++; + } + + numsSet[num] = 1; + windowSumm += num; + + if (maxSum < windowSumm){ + maxSum = windowSumm; + } + } + + return maxSum; +}