Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

247
Vistas
Calculate highest occupancy over time period

Hi im doing a job at work where we have a camera system that pings everytime someone enters or exits a room so you would have data like this:

{enter:7, exit:6}

which should mean one person is left in room

I want to know what was the highest amount of people in the room over a given period lets just say 45 minutes. I could have 50 to 100 points of data how can i would out what the average would be

Thanks

// update with a code sample here

.then((results) => {
  // maybe a correct algorithim

  results.sort((a, b) => b.enter - a.enter);

  results.sort((a, b) => b.exit - a.exit);

  results.sort((a, b) => a.enter - b.enter || b.exit - a.exit);

  // results.sort(fieldSorter(["enter"]));
  let final_result = result.pop();

  console.log(final_result);
  if (final_result) {
    return parseInt(final_result.enter - final_result.exit);
  } else {
    return 0;
  }
})

So before this code im gathering all the datapoints then try and sort them and pop off the highest entertence to the lowest exits but i think im going about this wrong

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Assuming that enter time is always smaller than exit time (which contradicts your example data), the issue could be solved like this:

function getAverage(startTime, endTime, items) {
    let slots = {};
    for (let timeIndex = startTime; timeIndex <= endTime; timeIndex++) {
        slots[timeIndex] = 0;
    }
    for (item of items) {
        let enter = Math.min(item.enter, startTime);
        let exit = Math.min(item.exit, endTime);
        for (let momentIndex = enter; momentIndex <= exit; momentIndex++) {
            slots[momentIndex]++;
        }
    }
    let avg = 0;
    for (let slot of slots) avg += slot;
    return avg / slots.length;
}

EDIT

For computing the maximum you can do something like this:

function getAverage(startTime, endTime, items) {
    let slots = {};
    for (let timeIndex = startTime; timeIndex <= endTime; timeIndex++) {
        slots[timeIndex] = 0;
    }
    for (item of items) {
        let enter = Math.min(item.enter, startTime);
        let exit = Math.min(item.exit, endTime);
        for (let momentIndex = enter; momentIndex <= exit; momentIndex++) {
            slots[momentIndex]++;
        }
    }
    let max = 0;
    for (let slot of slots) max = Math.max(max, slot);
    return max;
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda