还在写多标签查询的方法(谈不上算法),流程大概整理出来了:
1. 查询和构建数据阵列(限制最大查询长度),每个标签对应的数组都是顺序。
2. 使用双指针查询(效率高于二分法)找到多个标签中重合部分,并记录输出。
3. 查询阵列末尾时间戳中的最小值,二分法查询各标签在该时间戳后的数据并缓存。
4. 下次查询时,自刚才最小值开始,查询数量=需求数量-已缓存数据。
5. 重复上述查询过程,所有输出的重合部分即结果。
双指针查询写法(GPT生成):
function findIntersection(arrays) { if (arrays.length === 0) { return []; } const sortedArrays = arrays.map(arr => [...arr].sort((a, b) => a - b)); const intersection = []; const pointers = new Array(sortedArrays.length).fill(0); let done = false; while (!done) { const values = pointers.map((pointer, index) => sortedArrays[index][pointer]); const minValue = Math.min(...values); const maxValue = Math.max(...values); if (minValue === maxValue) { intersection.push(minValue); for (let i = 0; i < pointers.length; i++) { pointers[i]++; } } else { const minPointer = values.findIndex(value => value === minValue); if (minPointer === -1) { break; } pointers[minPointer]++; } done = pointers.some((pointer, index) => pointer === sortedArrays[index].length); } return intersection; } const arrays = [ [1, 3, 4, 6, 7, 9], [2, 3, 4, 7, 9, 10], [3, 4, 7, 8, 9, 11] ]; const intersection = findIntersection(arrays); console.log(intersection);
C大tql
365cent C大tql
最快本周完成Demo,上班摸鱼摸爽了
C大又回来啦
zzzz C大又回来啦
最近可以摸鱼……