-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeautifulTriplets.js
46 lines (38 loc) · 1009 Bytes
/
beautifulTriplets.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* Complete the 'beautifulTriplets' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER d
* 2. INTEGER_ARRAY arr
*/
function beautifulTriplets(d, arr) {
// Write your code here
let count = arr[0];
let max = arr[arr.length - 1];
let result = 0;
let { values } = new Array(arr.length).fill(0).reduce(
(target, item, index) => {
target["values"][arr[index]] = target["values"][arr[index]]
? (target["values"][arr[index]] += 1)
: 1;
return target;
},
{ values: {} }
);
while (count <= max) {
values[count] &&
values[count + d] &&
values[count + d * 2] &&
(result += Math.max(
values[count],
values[count + d],
values[count + d * 2]
));
count++;
}
return result;
}
let d=3;
let arr=[1, 2, 4, 5, 7, 8, 10];//3
console.log(beautifulTriplets(d, arr));