Leetcode Problem 1695. Maximum Erasure Value

1695. Maximum Erasure Value

Leetcode Solutions

Two Pointer Approach Using Boolean Array

  1. Initialize a boolean array isPresent of size 10001 (since 1 <= nums[i] <= 10^4).
  2. Initialize start and end pointers to 0, currentSum to 0, and maxSum to 0.
  3. Iterate over the array nums using the end pointer.
  4. For each element at end, check if it is already present in isPresent.
  5. If not present, mark it as present, add its value to currentSum, and update maxSum if currentSum is greater.
  6. If present, move the start pointer to the right, unmarking elements in isPresent and subtracting their values from currentSum until the duplicate element is removed.
  7. Continue the process until end has reached the end of nums.
  8. Return maxSum as the result.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...