File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 561
561
736|[ Parse Lisp Expression] ( ./0736-parse-lisp-expression.js ) |Hard|
562
562
738|[ Monotone Increasing Digits] ( ./0738-monotone-increasing-digits.js ) |Medium|
563
563
739|[ Daily Temperatures] ( ./0739-daily-temperatures.js ) |Medium|
564
+ 740|[ Delete and Earn] ( ./0740-delete-and-earn.js ) |Medium|
564
565
743|[ Network Delay Time] ( ./0743-network-delay-time.js ) |Medium|
565
566
744|[ Find Smallest Letter Greater Than Target] ( ./0744-find-smallest-letter-greater-than-target.js ) |Easy|
566
567
745|[ Prefix and Suffix Search] ( ./0745-prefix-and-suffix-search.js ) |Hard|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments