Skip to content

Commit 72c89cd

Browse files
add 1899
1 parent 4309361 commit 72c89cd

File tree

2 files changed

+61
-1
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src/main/java/com/fishercoder/solutions/secondthousand

2 files changed

+61
-1
lines changed

paginated_contents/algorithms/2nd_thousand/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
| 1909 | [Remove One Element to Make the Array Strictly Increasing](https://leetcode.com/problems/remove-one-element-to-make-the-array-strictly-increasing/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1909.java) || Easy | Array |
3232
| 1904 | [The Number of Full Rounds You Have Played](https://leetcode.com/problems/the-number-of-full-rounds-you-have-played/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1904.java) || Medium | String, Greedy |
3333
| 1903 | [Largest Odd Number in String](https://leetcode.com/problems/largest-odd-number-in-string/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1903.java) | [:tv:](https://youtu.be/IIt_ARZzclY) | Easy | Greedy |
34+
| 1899 | [Merge Triplets to Form Target Triplet](https://leetcode.com/problems/merge-triplets-to-form-target-triplet/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1899.java) || Medium | Array, Greedy |
3435
| 1897 | [Redistribute Characters to Make All Strings Equal](https://leetcode.com/problems/redistribute-characters-to-make-all-strings-equal/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1897.java) || Easy | String, Greedy |
3536
| 1893 | [Check if All the Integers in a Range Are Covered](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1893.java) || Easy | Array, HashTable, Prefix Sum |
3637
| 1891 | [Cutting Ribbons](https://leetcode.com/problems/cutting-ribbons/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1891.java) || Medium | Array, Binary Search |
@@ -348,7 +349,7 @@
348349
| 1260 | [Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1260.java) | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s) | Easy ||
349350
| 1258 | [Synonymous Sentences](https://leetcode.com/problems/synonymous-sentences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1258.java) || Medium | Backtracking |
350351
| 1257 | [Smallest Common Region](https://leetcode.com/problems/smallest-common-region/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1257.java) || Medium | Tree, HashTable, DFS, BFS |
351-
| 1254 | [Number of Closed Islands](https://leetcode.com/problems/number-of-closed-islands/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1254.java) || Medium | BFS |
352+
| 1254 | [Number of Closed Islands](https://leetcode.com/problems/number-of-closed-islands/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1254.java) || Medium | BFS |
352353
| 1252 | [Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1252.java) | | Easy ||
353354
| 1249 | [Minimum Remove to Make Valid Parentheses](https://leetcode.com/problems/minimum-remove-to-make-valid-parentheses/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1249.java) | | Medium | String, Stack |
354355
| 1243 | [Array Transformation](https://leetcode.com/problems/array-transformation/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1243.java) | [:tv:](https://www.youtube.com/watch?v=MQ2i4T1l-Gs) | Easy ||
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
public class _1899 {
4+
public static class Solution1 {
5+
public boolean mergeTriplets(int[][] triplets, int[] target) {
6+
int[] base = new int[3];
7+
int baseIndex = -1;
8+
for (int i = 0; i < triplets.length; i++) {
9+
if (findBaseTriplet(triplets[i], target)) {
10+
base = triplets[i];
11+
baseIndex = i;
12+
break;
13+
}
14+
}
15+
for (int i = 0; i < triplets.length; i++) {
16+
if (i != baseIndex) {
17+
boolean merged = false;
18+
if (shouldMerge(triplets[i], target, 0)) {
19+
merged = true;
20+
base = mergeTriplets(triplets[i], base);
21+
}
22+
if (!merged && shouldMerge(triplets[i], target, 1)) {
23+
merged = true;
24+
base = mergeTriplets(triplets[i], base);
25+
}
26+
if (!merged && shouldMerge(triplets[i], target, 2)) {
27+
base = mergeTriplets(triplets[i], base);
28+
}
29+
}
30+
}
31+
return base[0] == target[0] && base[1] == target[1] && base[2] == target[2];
32+
}
33+
34+
private int[] mergeTriplets(int[] triplet, int[] base) {
35+
return new int[]{Math.max(triplet[0], base[0]), Math.max(triplet[1], base[1]), Math.max(triplet[2], base[2])};
36+
}
37+
38+
private boolean shouldMerge(int[] triplet, int[] target, int i) {
39+
if (triplet[i] == target[i]) {
40+
//check the other two indexes not exceeding target
41+
if (i == 0) {
42+
return triplet[1] <= target[1] && triplet[2] <= target[2];
43+
} else if (i == 1) {
44+
return triplet[0] <= target[0] && triplet[2] <= target[2];
45+
} else if (i == 2) {
46+
return triplet[0] <= target[0] && triplet[1] <= target[1];
47+
}
48+
}
49+
return false;
50+
}
51+
52+
private boolean findBaseTriplet(int[] triplet, int[] target) {
53+
if (triplet[0] <= target[0] && triplet[1] <= target[1] && triplet[2] <= target[2]) {
54+
return true;
55+
}
56+
return false;
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)