Skip to content

Commit e9a9ab8

Browse files
committed
Add solution #1695
1 parent 8fa8883 commit e9a9ab8

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,481 LeetCode solutions in JavaScript
1+
# 1,482 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1306,6 +1306,7 @@
13061306
1690|[Stone Game VII](./solutions/1690-stone-game-vii.js)|Medium|
13071307
1691|[Maximum Height by Stacking Cuboids](./solutions/1691-maximum-height-by-stacking-cuboids.js)|Hard|
13081308
1694|[Reformat Phone Number](./solutions/1694-reformat-phone-number.js)|Easy|
1309+
1695|[Maximum Erasure Value](./solutions/1695-maximum-erasure-value.js)|Medium|
13091310
1716|[Calculate Money in Leetcode Bank](./solutions/1716-calculate-money-in-leetcode-bank.js)|Easy|
13101311
1718|[Construct the Lexicographically Largest Valid Sequence](./solutions/1718-construct-the-lexicographically-largest-valid-sequence.js)|Medium|
13111312
1726|[Tuple with Same Product](./solutions/1726-tuple-with-same-product.js)|Medium|
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 1695. Maximum Erasure Value
3+
* https://leetcode.com/problems/maximum-erasure-value/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array of positive integers nums and want to erase a subarray containing unique
7+
* elements. The score you get by erasing the subarray is equal to the sum of its elements.
8+
*
9+
* Return the maximum score you can get by erasing exactly one subarray.
10+
*
11+
* An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is,
12+
* if it is equal to a[l],a[l+1],...,a[r] for some (l,r).
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @return {number}
18+
*/
19+
var maximumUniqueSubarray = function(nums) {
20+
const seen = new Set();
21+
let result = 0;
22+
let currentSum = 0;
23+
let start = 0;
24+
25+
for (let end = 0; end < nums.length; end++) {
26+
while (seen.has(nums[end])) {
27+
seen.delete(nums[start]);
28+
currentSum -= nums[start];
29+
start++;
30+
}
31+
seen.add(nums[end]);
32+
currentSum += nums[end];
33+
result = Math.max(result, currentSum);
34+
}
35+
36+
return result;
37+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy