Skip to content

Commit f1457ea

Browse files
committed
Add solution #1534
1 parent 04f9d2c commit f1457ea

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,290 LeetCode solutions in JavaScript
1+
# 1,291 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1114,6 +1114,7 @@
11141114
1519|[Number of Nodes in the Sub-Tree With the Same Label](./solutions/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
11151115
1524|[Number of Sub-arrays With Odd Sum](./solutions/1524-number-of-sub-arrays-with-odd-sum.js)|Medium|
11161116
1528|[Shuffle String](./solutions/1528-shuffle-string.js)|Easy|
1117+
1534|[Count Good Triplets](./solutions/1534-count-good-triplets.js)|Easy|
11171118
1535|[Find the Winner of an Array Game](./solutions/1535-find-the-winner-of-an-array-game.js)|Medium|
11181119
1550|[Three Consecutive Odds](./solutions/1550-three-consecutive-odds.js)|Easy|
11191120
1551|[Minimum Operations to Make Array Equal](./solutions/1551-minimum-operations-to-make-array-equal.js)|Medium|

solutions/1534-count-good-triplets.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* 1534. Count Good Triplets
3+
* https://leetcode.com/problems/count-good-triplets/
4+
* Difficulty: Easy
5+
*
6+
* Given an array of integers arr, and three integers a, b and c. You need to find the number
7+
* of good triplets.
8+
*
9+
* A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:
10+
* - 0 <= i < j < k < arr.length
11+
* - |arr[i] - arr[j]| <= a
12+
* - |arr[j] - arr[k]| <= b
13+
* - |arr[i] - arr[k]| <= c
14+
*
15+
* Where |x| denotes the absolute value of x.
16+
*
17+
* Return the number of good triplets.
18+
*/
19+
20+
function countGoodTriplets(arr, a, b, c) {
21+
const length = arr.length;
22+
let result = 0;
23+
24+
for (let i = 0; i < length - 2; i++) {
25+
for (let j = i + 1; j < length - 1; j++) {
26+
if (Math.abs(arr[i] - arr[j]) <= a) {
27+
for (let k = j + 1; k < length; k++) {
28+
if (Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) {
29+
result++;
30+
}
31+
}
32+
}
33+
}
34+
}
35+
36+
return result;
37+
}

0 commit comments

Comments
 (0)