Files
C/leetcode/src/1695.c
Alexander Pantyukhin 2ee92040f3 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 <tjgurwara99@users.noreply.github.com>

Co-authored-by: David Leal <halfpacho@gmail.com>
Co-authored-by: Taj <tjgurwara99@users.noreply.github.com>
2022-12-01 23:18:59 -06:00

30 lines
715 B
C

// 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;
}