mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-25 14:59:36 +03:00
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>
This commit is contained in:
parent
b37bf7f6b9
commit
2ee92040f3
@ -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 |
|
||||
|
29
leetcode/src/1695.c
Normal file
29
leetcode/src/1695.c
Normal file
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user