deno.land / x / masx200_leetcode_test@10.6.5 / find-a-value-of-a-mysterious-function-closest-to-target / index.ts
1234567891011121314151617181920212223242526272829export 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;}
Version Info