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