English 中文(简体)
Java文中的有效go果是合并日期序列?
原标题:What would be an efficient algo in JavaScript to merge date time series?

我有以下时间序列清单作为投入:

const ts1 = [
  [ 2023-01-20 , 1],
  [ 2023-01-21 , 2],
  [ 2023-01-22 , 3],
  [ 2023-01-23 , 4],
];

const ts2 = [
  [ 2023-01-18 , 5],
  [ 2023-01-19 , 6],
  [ 2023-01-20 , 7],
  [ 2023-01-21 , 8]
];

const ts3 = [
  [ 2023-01-21 , 9],
  [ 2023-01-22 , 10],
  [ 2023-01-23 , 11],
  [ 2023-01-24 , 12]
];

我把产出同合并(栏目打)一样:

const output = [
  [ 2023-01-18 , null,    5, null],
  [ 2023-01-19 , null,    6, null],
  [ 2023-01-20 ,    1,    7, null],
  [ 2023-01-21 ,    2,    8,    9],
  [ 2023-01-22 ,    3, null,   10],
  [ 2023-01-23 ,    4, null,   11],
  [ 2023-01-24 , null, null,   12]
];

为了给更多的环境,我重新使用提供个人时间序列的科技创新工具,我需要汇编所需的表格Data格式。

最佳回答

我这样说。

const merged = {};

let tses = [ts1, ts2, ts3];
for (let i = 0; i < tses.length; i++) {
  for (const [t, v] of tses[i]) {
    if (!merged[t]) merged[t] = new Array(tses.length).fill(null);
    merged[t][i] = v;
  }
}

console.log(Object.entries(merged).map(([t, vs]) => [t, ...vs]));

产出

[
  [  2023-01-20 , 1, 7, null ],
  [  2023-01-21 , 2, 8, 9 ],
  [  2023-01-22 , 3, null, 10 ],
  [  2023-01-23 , 4, null, 11 ],
  [  2023-01-18 , null, 5, null ],
  [  2023-01-19 , null, 6, null ],
  [  2023-01-24 , null, null, 12 ]
]

预计。

分类顺序(因为钥匙在地理上是可比较的),确实

console.log(Object.keys(merged).sort().map(key => [key, ...merged[key]]));

相反:

[
  [  2023-01-18 , null, 5, null ],
  [  2023-01-19 , null, 6, null ],
  [  2023-01-20 , 1, 7, null ],   
  [  2023-01-21 , 2, 8, 9 ],      
  [  2023-01-22 , 3, null, 10 ],  
  [  2023-01-23 , 4, null, 11 ],
  [  2023-01-24 , null, null, 12 ]
]
问题回答

http://developer.mozilla.org

const ts1=[["2023-01-20",1],["2023-01-21",2],["2023-01-22",3],["2023-01-23",4],],ts2=[["2023-01-18",5],["2023-01-19",6],["2023-01-20",7],["2023-01-21",8]],ts3=[["2023-01-21",9],["2023-01-22",10],["2023-01-23",11],["2023-01-24",12]];
const res = Object.entries([ts1, ts2, ts3].reduce((acc, curr, i, t) => {
  curr.forEach(([k, v]) => (acc[k] ??= Array(t.length).fill(null))[i] = v);
  return acc;
}, {})).sort().map(([k, v]) => [k, ...v]);
console.log(res);

你可以建立一个高效的算法,先把所有日期和价值列入一个黑体地图,然后将其转化为一个阵列,从而合并时间序列。

这是一种有效的方法,因为散射地图操作(扫描和勘测)通常为O(1),使算法比直接比较和插入阵列更快。

例如:

const ts1 = [
  [ 2023-01-20 , 1],
  [ 2023-01-21 , 2],
  [ 2023-01-22 , 3],
  [ 2023-01-23 , 4],
];

const ts2 = [
  [ 2023-01-18 , 5],
  [ 2023-01-19 , 6],
  [ 2023-01-20 , 7],
  [ 2023-01-21 , 8]
];

const ts3 = [
  [ 2023-01-21 , 9],
  [ 2023-01-22 , 10],
  [ 2023-01-23 , 11],
  [ 2023-01-24 , 12]
];

function mergeTimeSeries(...timeSeries) {
  const hashMap = {};
  let seriesIndex = 1;
  
  for (const ts of timeSeries) {
    for (const [date, value] of ts) {
      if (!hashMap[date]) {
        hashMap[date] = Array(timeSeries.length + 1).fill(null);
        hashMap[date][0] = date;
      }
      hashMap[date][seriesIndex] = value;
    }
    seriesIndex++;
  }

  // Convert hashMap into array and sort by date
  const result = Object.values(hashMap).sort((a, b) => new Date(a[0]) - new Date(b[0]));

  return result;
}

const output = mergeTimeSeries(ts1, ts2, ts3);

console.log(output);

如果没有目标,也可以这样做。 条目和钥匙等。

例如......

let res = [], pos = {};

[ts1, ts2, ts3].forEach((a, i, arr) =>
  a.forEach(([k, v]) => {
    pos[k] = pos[k] || (res[res.length] = [k, ...arr.map(_ => null)]);
    pos[k][i + 1] = v;
  }));

console.log(res.sort());




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签