-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete and earn
44 lines (44 loc) · 1.05 KB
/
delete and earn
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
class Solution {
public int deleteAndEarn(int[] nums) {
Arrays.sort(nums);
int arr[] = new int[nums[nums.length-1]];
int j=0;
arr[0] = nums[0];
for(int i=0;i<nums.length-1;i++)
{
if(nums[i+1]>nums[i]+1)
{
for(int k = nums[i];k<nums[i+1] - 1;k++)
{
j++;
arr[j] = 0;
}
}
if(nums[i] == nums[i+1])
arr[j] += nums[i];
else if(j<arr.length-1){
j++;
arr[j] = nums[i+1];
}
}
int sum=0;
int sum1=0;
for(int i=0;i<arr.length;i++)
{
if(i%2 == 0)
{
sum += arr[i];
sum = Math.max(sum,sum1);
}
else
{
sum1 += arr[i];
sum1 = Math.max(sum,sum1);
}
}
if(sum1>sum)
return sum1;
else
return sum;
}
}