Skip to content

Commit 32b45fd

Browse files
solves count good tripets
1 parent b6a7f8d commit 32b45fd

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,6 @@
386386
| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles) | [![Java](assets/java.png)](src/WaterBottles.java) | |
387387
| 1523 | [Count Odd Numbers In Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range) | [![Java](assets/java.png)](src/CountOddNumbersInIntervalRange.java) | |
388388
| 1528 | [Shuffle Strings](https://leetcode.com/problems/shuffle-string) | [![Java](assets/java.png)](src/ShuffleString.java) | |
389-
| 1534 | [Count Good Triplets](https://leetcode.com/problems/count-good-triplets) | | |
389+
| 1534 | [Count Good Triplets](https://leetcode.com/problems/count-good-triplets) | [![Java](assets/java.png)](src/CountGoodTriplets.java) | |
390390
| 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number) | | |
391391
| 1544 | [Make The String Great](https://leetcode.com/problems/make-the-string-great) | | |

src/CountGoodTriplets.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/count-good-triplets
2+
// T: O(n^3)
3+
// S: O(1)
4+
5+
public class CountGoodTriplets {
6+
public int countGoodTriplets(int[] arr, int a, int b, int c) {
7+
int goodTriplets = 0;
8+
for (int i = 0 ; i < arr.length ; i++) {
9+
for (int j = i + 1 ; j < arr.length ; j++) {
10+
for (int k = j + 1 ; k < arr.length ; k++) {
11+
if (Math.abs(arr[i] - arr[j]) <= a && Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) {
12+
goodTriplets++;
13+
}
14+
}
15+
}
16+
}
17+
return goodTriplets;
18+
}
19+
}

0 commit comments

Comments
 (0)