forked from super30admin/DP-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem1.java
45 lines (35 loc) · 1.05 KB
/
Problem1.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Time Complexity: O(maxele,maxeleinarray)
//Space Complexity :O(maxele) maxele in array
public class Problem1 {
public static int deleteAndEarn(int[] nums) {
if(nums==null || nums.length==0)
return 0;
int maxnum= getmax(nums);
int[] sortedarr = new int[maxnum+1];
for(int i=0;i<nums.length;i++){
sortedarr[nums[i]]+=nums[i];
}
int c=0;
int dc=0;
for(int i=0;i<sortedarr.length;i++){
int temp=dc;
dc= Math.max(c,dc);
c=temp+sortedarr[i];
}
return Math.max(c,dc);
}
private static int getmax(int[] nums){
int max= Integer.MIN_VALUE;
for(int i: nums){
if(max<i){
max=i;
}
}
return max;
}
public static void main(String[] args) {
int nums[] = new int[] {3,4,2};
int res= deleteAndEarn(nums);
System.out.println(res);
}
}