File tree 1 file changed +12
-11
lines changed
scripts/algorithms/D/Delete and Earn
1 file changed +12
-11
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 44 ms (Top 16.30%) | Memory: 16.4 MB (Top 21.44%)
1
2
2
3
class Solution {
3
4
public:
4
5
// Dynamic Programming : Bottom Up Approach Optmised
5
- int deleteAndEarn (vector<int >& nums)
6
+ int deleteAndEarn (vector<int >& nums)
6
7
{
7
8
int size = nums.size ();
8
-
9
- // Initialise vectors with size 10001 and value 0
9
+
10
+ // Initialise vectors with size 10001 and value 0
10
11
vector<int > memo (10001 , 0 );
11
12
vector<int > res (10001 , 0 );
12
-
13
- // get the count of elements int res vector
13
+
14
+ // get the count of elements int res vector
14
15
for (auto num : nums)
15
16
res[num]++;
16
-
17
- // for index : less than 3 calculate memo[i] as the index times number of occurences
18
- // : greater than equal to 3 as calculate memo[i] as the index times number of occurences + max of the last second and third element
17
+
18
+ // for index : less than 3 calculate memo[i] as the index times number of occurences
19
+ // : greater than equal to 3 as calculate memo[i] as the index times number of occurences + max of the last second and third element
19
20
for (int i = 0 ; i < 10001 ; i++)
20
21
memo[i] += (i < 3 ) ? (i * res[i]) : (i * res[i]) + max (memo[i-2 ] , memo[i-3 ]);
21
-
22
- // return max of last 2 elements
22
+
23
+ // return max of last 2 elements
23
24
return max (memo[10000 ] , memo[9999 ]);
24
25
}
25
- };
26
+ };
You can’t perform that action at this time.
0 commit comments