-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathProblem1.java
More file actions
29 lines (28 loc) · 850 Bytes
/
Copy pathProblem1.java
File metadata and controls
29 lines (28 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Time Complexity : O(n + maxValue)
// Space Complexity : O(maxValue)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
class Solution {
public int deleteAndEarn(int[] nums) {
// Edge case
if (nums.length== 0) return 0;
int max=0;
for (int n:nums) {
max = Math.max(max, n);
}
// points[i] = total points we get if we take number i
int[] points = new int[max + 1];
for (int n : nums) {
points[n]=points[n]+n;
}
// Same as house robber
int prev2 = 0;// dp[i-2]
int prev1 = points[0]; // dp[i-1]
for (int i=1;i<= max;i++) {
int curr = Math.max(prev1,prev2+points[i]);
prev2=prev1;
prev1=curr;
}
return prev1;
}
}