Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a28a58e

Browse files
committed
1534
1 parent 20eedcd commit a28a58e

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
class Solution
3+
{
4+
/**
5+
* @param Integer[] $arr
6+
* @param Integer $a
7+
* @param Integer $b
8+
* @param Integer $c
9+
* @return Integer
10+
*/
11+
public function countGoodTriplets($arr, $a, $b, $c)
12+
{
13+
$count = 0;
14+
$n = count($arr);
15+
for ($i = 0; $i < $n - 2; $i++) {
16+
for ($j = $i + 1; $j < $n - 1; $j++) {
17+
18+
if (abs($arr[$i] - $arr[$j]) > $a) {
19+
continue;
20+
}
21+
22+
for ($k = $j + 1; $k < $n; $k++) {
23+
24+
if (
25+
abs($arr[$j] - $arr[$k]) <= $b && abs(($arr[$i] - $arr[$k])) <= $c) {
26+
$count++;
27+
}
28+
}
29+
}
30+
}
31+
return $count;
32+
}
33+
}
34+
35+
$solution = new Solution();
36+
37+
$arr = [3, 0, 1, 1, 9, 7];
38+
39+
$a = 7;
40+
41+
$b = 2;
42+
$c = 3;
43+
$result = $solution->countGoodTriplets($arr, $a, $b, $c);
44+
45+
echo "The number of good triplets is: " . $result . "\n"; // Output: 4

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /