-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectSortTest.java
More file actions
100 lines (78 loc) · 1.92 KB
/
Copy pathObjectSortTest.java
File metadata and controls
100 lines (78 loc) · 1.92 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package ds;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.Comparator;
import org.junit.jupiter.api.Test;
class ObjectSortTest {
class Player implements Comparator<Player> {
String name;
int score;
public Player(String name, int score) {
this.name = name;
this.score = score;
}
@Override
public int compare(Player a, Player b) {
if (a.score == b.score) {
return a.name.compareTo(b.name);
}
return b.score - a.score; // reverse order by score
}
}
class Circle implements Comparable<Circle> {
public double radius;
public Circle(double r) {
this.radius = r;
}
public Circle() {
this(1);
}
@Override
public int compareTo(Circle o) {
if (this.radius > o.radius) return 1;
if (this.radius == o.radius) return 0;
return -1;
}
@Override
public String toString() {
return "This Circle is of radius: " + this.radius;
}
}
@Test
void testSortCircle() {
//fail("Not yet implemented");
}
@Test
// 2D Array of games of (luck, importance),
// find Max luck balance given K loss of important games
void testSortArray() {
int[][] contests =
{
{6,1},
{5,0},
{4,1},
{2,0}
};
System.out.println(luckBalance(2, contests));
}
static int luckBalance (int k, int[][] contests) {
int luckBalance = 0;
Arrays.sort(contests, new Comparator<int[]> () {
@Override
public int compare(int[] a, int[] b) {
return -1 * Integer.compare(a[0], b[0]);
}
});
for (int i = 0; i < contests.length; i++) {
int luck = contests[i][0];
int importance = contests[i][1];
if(importance == 1 && k > 0) {
k--; luckBalance += luck; // lose to get luck point
} else if (importance == 1 && k == 0) {
luckBalance -= luck; // has to win, can't lose
}
if (importance == 0) luckBalance += luck; // lose to get luck point
}
return luckBalance;
}
}