-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0740-delete-and-earn.js
More file actions
35 lines (31 loc) · 922 Bytes
/
0740-delete-and-earn.js
File metadata and controls
35 lines (31 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 740. Delete and Earn
* https://leetcode.com/problems/delete-and-earn/
* Difficulty: Medium
*
* You are given an integer array nums. You want to maximize the number of points you get by
* performing the following operation any number of times:
* - Pick any nums[i] and delete it to earn nums[i] points. Afterwards, you must delete every
* element equal to nums[i] - 1 and every element equal to nums[i] + 1.
*
* Return the maximum number of points you can earn by applying the above operation some number
* of times.
*/
/**
* @param {number[]} nums
* @return {number}
*/
var deleteAndEarn = function(nums) {
const points = new Array(10001).fill(0);
let previous = 0;
let result = 0;
for (const num of nums) {
points[num] += num;
}
for (const value of points) {
const temp = result;
result = Math.max(result, previous + value);
previous = temp;
}
return result;
};