简体   繁体   English

计算碰撞数的个数

[英]Calculate number of colliding numbers

I have a dataset of events for a calendar.我有一个日历事件数据集。 It is going to be placed into a grid.它将被放置在一个网格中。

I am trying to count the highest numbers of events that collide at the same time.我正在尝试计算同时发生碰撞的事件的最高数量。 Every event has a start time and end time.每个事件都有start时间和end时间。

What I have done so far is count the number of matches, but this won't find the maximum of collisions..到目前为止我所做的是计算匹配的数量,但这不会找到最大的碰撞..

 const events = [{ id: 0, start: 6, end: 9, title: "Hello" }, { id: 1, start: 6, end: 9, title: "Hello" }, { id: 2, start: 7, end: 8, title: "Hello" }, { id: 3, start: 10, end: 15, title: "Hello" }, { id: 4, start: 20, end: 25, title: "Hello" }, { id: 5, start: 21, end: 23, title: "Hello" }, ] // Check how many events are colliding at same time, to make more columns let items = [] events.map(event => { const matches = events.filter(o => { //console.log(o.start, "is smaller or same", event.start, o.start <= event.end) return o.start <= event.end && o.end >= event.start && event.id.== o.id }) items.push({..,event. matches }) }) const collitions = items,sort((a. b) => { if (a.matches < b.matches) return 1 if (a.matches > b.matches) return -1 return 0 })[0].matches.length + 1 console,log('There is', collitions, 'collitions')

Here is the grid when the calculation is using the events data in the example.这是计算使用示例中的事件数据时的网格。 It outputs three, which is correct.它输出三个,这是正确的。

在此处输入图像描述

Here is the grid when there is an event which is the whole day.这是全天发生事件时的网格。 It outputs 6 colliding events, but that's wrong.它输出 6 个碰撞事件,但这是错误的。 It is 4 colliding events in a row.这是连续 4 次碰撞事件。

在此处输入图像描述

Here is a simpler solution.这是一个更简单的解决方案。

Approach方法

  • Have an array of length 24 so that each value will represent 1 hour有一个长度为 24 的数组,以便每个值代表 1 小时
  • Loop through the each event and increment the value at each hour for the duration循环遍历每个事件并在持续时间内的每个小时递增值
  • At the end find the max value in the array最后找到数组中的最大值

Code代码

 const events = [{ id: 0, start: 6, end: 9, title: "Hello" }, { id: 1, start: 6, end: 9, title: "Hello" }, { id: 2, start: 7, end: 8, title: "Hello" }, { id: 3, start: 10, end: 15, title: "Hello" }, { id: 4, start: 20, end: 25, title: "Hello" }, { id: 5, start: 21, end: 23, title: "Hello" }, ] const array = new Array(24).fill(0) events.forEach(function(event) { for (let i = event.start; i < event.end; ++i) { ++array[i] } }) let maxEvents = -1 for (let i = 0; i< array.length; ++i) { if (array[i] > maxEvents) maxEvents = array[i] } console.log("collisions: ", maxEvents) console.log("collision array:", JSON.stringify(array))

Have checked it for case when event.start = 0 and event.end = 23, and it is giving the correct output已经检查了 event.start = 0 和 event.end = 23 的情况,它给出了正确的输出

Note:笔记:

The solution will also work in case start and end time are hm:mm .如果开始和结束时间是hm:mm ,该解决方案也将起作用。 In this case, we need to have array of length 24*60 (hours*mins) .在这种情况下,我们需要长度为24*60 (hours*mins)的数组。 Also, i inside for loop will be the total number of minutes starting from 0 (eg 2:45 mean i = 165 ).此外,for 循环中的i将是从 0 开始的总分钟数(例如2:45 mean i = 165 )。

Hope it helps.希望能帮助到你。 Revert for any doubts.如有任何疑问,请回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM