- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1899.java
63 lines (59 loc) · 2.37 KB
/
_1899.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
59
60
61
62
63
packagecom.fishercoder.solutions.secondthousand;
publicclass_1899 {
publicstaticclassSolution1 {
publicbooleanmergeTriplets(int[][] triplets, int[] target) {
int[] base = newint[3];
intbaseIndex = -1;
for (inti = 0; i < triplets.length; i++) {
if (findBaseTriplet(triplets[i], target)) {
base = triplets[i];
baseIndex = i;
break;
}
}
for (inti = 0; i < triplets.length; i++) {
if (i != baseIndex) {
booleanmerged = false;
if (shouldMerge(triplets[i], target, 0)) {
merged = true;
base = mergeTriplets(triplets[i], base);
}
if (!merged && shouldMerge(triplets[i], target, 1)) {
merged = true;
base = mergeTriplets(triplets[i], base);
}
if (!merged && shouldMerge(triplets[i], target, 2)) {
base = mergeTriplets(triplets[i], base);
}
}
}
returnbase[0] == target[0] && base[1] == target[1] && base[2] == target[2];
}
privateint[] mergeTriplets(int[] triplet, int[] base) {
returnnewint[] {
Math.max(triplet[0], base[0]),
Math.max(triplet[1], base[1]),
Math.max(triplet[2], base[2])
};
}
privatebooleanshouldMerge(int[] triplet, int[] target, inti) {
if (triplet[i] == target[i]) {
// check the other two indexes not exceeding target
if (i == 0) {
returntriplet[1] <= target[1] && triplet[2] <= target[2];
} elseif (i == 1) {
returntriplet[0] <= target[0] && triplet[2] <= target[2];
} elseif (i == 2) {
returntriplet[0] <= target[0] && triplet[1] <= target[1];
}
}
returnfalse;
}
privatebooleanfindBaseTriplet(int[] triplet, int[] target) {
if (triplet[0] <= target[0] && triplet[1] <= target[1] && triplet[2] <= target[2]) {
returntrue;
}
returnfalse;
}
}
}