-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathStone Game VI.java
59 lines (51 loc) · 1.09 KB
/
Stone Game VI.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
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution
{
static class Pair
{
int sum=0;
int alice=0;
int bob=0;
public Pair(int sum,int alice, int bob)
{
this.sum=sum;
this.alice = alice;
this.bob = bob;
}
}
// class to define user defined conparator
static class Compare {
static void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
return p2.sum - p1.sum;
}
});
}
}
public int stoneGameVI(int[] aliceValues, int[] bobValues)
{
int n=aliceValues.length;
Pair[] a=new Pair[n];
for(int i=0;i<n;i++)
{
a[i]=new Pair(aliceValues[i]+bobValues[i],aliceValues[i],bobValues[i]);
}
Compare.compare(a,n);
int al=0;int bo=0;
for(int i=0;i<n;i++)
{
if(i%2==0)
{
al+=a[i].alice;
}
else
{
bo+=a[i].bob;
}
}
return Integer.compare(al,bo);
}
}