deno.land / x / masx200_leetcode_test@10.6.5 / find-a-value-of-a-mysterious-function-closest-to-target / index.ts

نووسراو ببینە
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
export default function closestToTarget(arr: number[], target: number): number { const temp: number[] = [];
for (const n of arr) { if (temp.length) { const last = temp[temp.length - 1]; if (last !== n) { temp.push(n); } } else { temp.push(n); } }
arr = temp;
let ans = Math.abs(arr[0] - target);
let valid = Array.of(arr[0]);
for (const num of arr) { const tv = new Set([num, ...valid.map((x) => x & num)]); valid = Array.from(tv);
ans = Math.min(ans, ...valid.map((x) => Math.abs(x - target))); } return ans;}
masx200_leetcode_test

Version Info

Tagged at
a year ago