Skip to content

Commit a368ada

Browse files
committed
Runtime: 44 ms (Top 16.30%) | Memory: 16.4 MB (Top 21.44%)
1 parent 22611f8 commit a368ada

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1+
// Runtime: 44 ms (Top 16.30%) | Memory: 16.4 MB (Top 21.44%)
12

23
class Solution {
34
public:
45
// Dynamic Programming : Bottom Up Approach Optmised
5-
int deleteAndEarn(vector<int>& nums)
6+
int deleteAndEarn(vector<int>& nums)
67
{
78
int size = nums.size();
8-
9-
// Initialise vectors with size 10001 and value 0
9+
10+
// Initialise vectors with size 10001 and value 0
1011
vector<int> memo(10001 , 0);
1112
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
1415
for(auto num : nums)
1516
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
1920
for(int i = 0 ; i < 10001 ; i++)
2021
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
2324
return max(memo[10000] , memo[9999]);
2425
}
25-
};
26+
};

0 commit comments

Comments
 (0)