Skip to content

Commit ce50062

Browse files
committed
Add solution #740
1 parent fdf9441 commit ce50062

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@
561561
736|[Parse Lisp Expression](./0736-parse-lisp-expression.js)|Hard|
562562
738|[Monotone Increasing Digits](./0738-monotone-increasing-digits.js)|Medium|
563563
739|[Daily Temperatures](./0739-daily-temperatures.js)|Medium|
564+
740|[Delete and Earn](./0740-delete-and-earn.js)|Medium|
564565
743|[Network Delay Time](./0743-network-delay-time.js)|Medium|
565566
744|[Find Smallest Letter Greater Than Target](./0744-find-smallest-letter-greater-than-target.js)|Easy|
566567
745|[Prefix and Suffix Search](./0745-prefix-and-suffix-search.js)|Hard|

solutions/0740-delete-and-earn.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 740. Delete and Earn
3+
* https://leetcode.com/problems/delete-and-earn/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums. You want to maximize the number of points you get by
7+
* performing the following operation any number of times:
8+
* - Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every
9+
* element equal to nums[i] - 1 and every element equal to nums[i] + 1.
10+
*
11+
* Return the maximum number of points you can earn by applying the above operation some number
12+
* of times.
13+
*/
14+
15+
/**
16+
* @param {number[]} nums
17+
* @return {number}
18+
*/
19+
var deleteAndEarn = function(nums) {
20+
const points = new Array(10001).fill(0);
21+
let previous = 0;
22+
let result = 0;
23+
24+
for (const num of nums) {
25+
points[num] += num;
26+
}
27+
28+
for (const value of points) {
29+
const temp = result;
30+
result = Math.max(result, previous + value);
31+
previous = temp;
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)