File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int deleteAndEarn (int [] nums ) {
3
+ Map <Integer , Integer > counter = new HashMap <>();
4
+ for (int i = 0 ; i < nums .length ; i ++) {
5
+ counter .put (nums [i ], counter .getOrDefault (nums [i ], 0 ) + 1 );
6
+ }
7
+ List <Integer > numsList = new ArrayList <>(counter .keySet ());
8
+ Collections .sort (numsList );
9
+
10
+ int earnOne = 0 ;
11
+ int earnTwo = 0 ;
12
+ for (int i = 0 ; i < numsList .size (); i ++) {
13
+ int curEarn = numsList .get (i ) * counter .get (numsList .get (i ));
14
+ if (i > 0 && numsList .get (i ) == numsList .get (i - 1 ) + 1 ) {
15
+ int temp = earnTwo ;
16
+ earnTwo = Math .max (earnOne + curEarn , earnTwo );
17
+ earnOne = temp ;
18
+ } else {
19
+ earnOne = earnTwo ;
20
+ earnTwo += curEarn ;
21
+ }
22
+ }
23
+ return earnTwo ;
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments