mirror of
https://github.com/TheAlgorithms/C.git
synced 2026-02-13 21:42:29 +00:00
* 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>
30 lines
715 B
C
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;
|
|
}
|