File tree 2 files changed +39
-1
lines changed
2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,290 LeetCode solutions in JavaScript
1
+ # 1,291 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1114
1114
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|
1115
1115
1524|[ Number of Sub-arrays With Odd Sum] ( ./solutions/1524-number-of-sub-arrays-with-odd-sum.js ) |Medium|
1116
1116
1528|[ Shuffle String] ( ./solutions/1528-shuffle-string.js ) |Easy|
1117
+ 1534|[ Count Good Triplets] ( ./solutions/1534-count-good-triplets.js ) |Easy|
1117
1118
1535|[ Find the Winner of an Array Game] ( ./solutions/1535-find-the-winner-of-an-array-game.js ) |Medium|
1118
1119
1550|[ Three Consecutive Odds] ( ./solutions/1550-three-consecutive-odds.js ) |Easy|
1119
1120
1551|[ Minimum Operations to Make Array Equal] ( ./solutions/1551-minimum-operations-to-make-array-equal.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments