From 4c8a3c94a55f59db60a12fca86885f898f6af3bb Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Tue, 8 Nov 2022 22:37:05 +0400 Subject: [PATCH 1/2] add leetcode Maximum Erasure Value --- leetcode/README.md | 1 + leetcode/src/1695.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 leetcode/src/1695.c diff --git a/leetcode/README.md b/leetcode/README.md index 216885c0c8..373d13b3da 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -96,4 +96,5 @@ | 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](Maximum Erasure Value) | [C](./src/1695.c) | Medium | | 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium | diff --git a/leetcode/src/1695.c b/leetcode/src/1695.c new file mode 100644 index 0000000000..e8f805f6b8 --- /dev/null +++ b/leetcode/src/1695.c @@ -0,0 +1,30 @@ + +// 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; +} \ No newline at end of file From 0727336604adb5db34f8a1925c2c4d1df58c7f53 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Tue, 8 Nov 2022 22:37:05 +0400 Subject: [PATCH 2/2] add leetcode Maximum Erasure Value --- leetcode/README.md | 1 + leetcode/src/1695.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 leetcode/src/1695.c diff --git a/leetcode/README.md b/leetcode/README.md index 216885c0c8..373d13b3da 100644 --- a/leetcode/README.md +++ b/leetcode/README.md @@ -96,4 +96,5 @@ | 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](Maximum Erasure Value) | [C](./src/1695.c) | Medium | | 2130 | [Maximum Twin Sum of a Linked List](https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/) | [C](./src/2130.c) | Medium | diff --git a/leetcode/src/1695.c b/leetcode/src/1695.c new file mode 100644 index 0000000000..e8f805f6b8 --- /dev/null +++ b/leetcode/src/1695.c @@ -0,0 +1,30 @@ + +// 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; +} \ No newline at end of file