|
| 1 | +/** |
| 2 | + * @param {number[]} nums |
| 3 | + * @return {number} |
| 4 | + */ |
| 5 | +const mi = Math.min, |
| 6 | + abs = Math.abs |
| 7 | +const minimumDifference = (nums) => { |
| 8 | + let m = nums.length, |
| 9 | + n = m >> 1 |
| 10 | + let a = initializeGraph(n + 1) |
| 11 | + let b = initializeGraph(n + 1) |
| 12 | + for (let i = 0; i < 1 << n; i++) { |
| 13 | + // mask |
| 14 | + let sum = 0, |
| 15 | + cnt = 0 |
| 16 | + for (let j = 0; j < n; j++) { |
| 17 | + if (i & (1 << j)) { |
| 18 | + // bit of 1's |
| 19 | + sum += nums[j] |
| 20 | + cnt++ // bit count |
| 21 | + } else { |
| 22 | + sum -= nums[j] |
| 23 | + } |
| 24 | + } |
| 25 | + a[cnt].push(sum) |
| 26 | + ;(sum = 0), (cnt = 0) |
| 27 | + for (let j = 0; j < n; j++) { |
| 28 | + if (i & (1 << j)) { |
| 29 | + sum += nums[n + j] |
| 30 | + cnt++ |
| 31 | + } else { |
| 32 | + sum -= nums[n + j] |
| 33 | + } |
| 34 | + } |
| 35 | + b[cnt].push(sum) |
| 36 | + } |
| 37 | + for (let i = 0; i < n; i++) { |
| 38 | + a[i].sort((x, y) => x - y) |
| 39 | + b[i].sort((x, y) => x - y) |
| 40 | + } |
| 41 | + let res = Number.MAX_SAFE_INTEGER |
| 42 | + let bi = new Bisect() |
| 43 | + for (let i = 0; i <= n; i++) { |
| 44 | + for (const x of a[i]) { |
| 45 | + let idx = bi.bisect_left(b[n - i], -x) // binary search lower_bound |
| 46 | + if (idx != b[n - i].length) res = mi(res, abs(x + b[n - i][idx])) |
| 47 | + if (idx != 0) { |
| 48 | + idx-- |
| 49 | + res = mi(res, abs(x + b[n - i][idx])) |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return res |
| 54 | +} |
| 55 | + |
| 56 | +//////////////////////////////////////// Template //////////////////////////////////////////////////////// |
| 57 | +function Bisect() { |
| 58 | + return { insort_right, insort_left, bisect_left, bisect_right } |
| 59 | + function insort_right(a, x, lo = 0, hi = null) { |
| 60 | + lo = bisect_right(a, x, lo, hi) |
| 61 | + a.splice(lo, 0, x) |
| 62 | + } |
| 63 | + function bisect_right(a, x, lo = 0, hi = null) { |
| 64 | + // > upper_bound |
| 65 | + if (lo < 0) throw new Error('lo must be non-negative') |
| 66 | + if (hi == null) hi = a.length |
| 67 | + while (lo < hi) { |
| 68 | + let mid = (lo + hi) >> 1 |
| 69 | + x < a[mid] ? (hi = mid) : (lo = mid + 1) |
| 70 | + } |
| 71 | + return lo |
| 72 | + } |
| 73 | + function insort_left(a, x, lo = 0, hi = null) { |
| 74 | + lo = bisect_left(a, x, lo, hi) |
| 75 | + a.splice(lo, 0, x) |
| 76 | + } |
| 77 | + function bisect_left(a, x, lo = 0, hi = null) { |
| 78 | + // >= lower_bound |
| 79 | + if (lo < 0) throw new Error('lo must be non-negative') |
| 80 | + if (hi == null) hi = a.length |
| 81 | + while (lo < hi) { |
| 82 | + let mid = (lo + hi) >> 1 |
| 83 | + a[mid] < x ? (lo = mid + 1) : (hi = mid) |
| 84 | + } |
| 85 | + return lo |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +const initializeGraph = (n) => { |
| 90 | + let G = [] |
| 91 | + for (let i = 0; i < n; i++) { |
| 92 | + G.push([]) |
| 93 | + } |
| 94 | + return G |
| 95 | +} |
| 96 | +///////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 97 | + |
0 commit comments