J
J
JIscariot2018-02-17 02:51:57
PHP
JIscariot, 2018-02-17 02:51:57

How to fill time gaps with the greatest efficiency?

There are 2 arrays. The first contains free time intervals. Second tasks and their duration.
It is necessary to assign each period of time its / its own task with maximum efficiency.

<?php

$periods = [
    [
        'start' =>  \DateTime::createFromFormat(
            'Y-m-d H:i:s', '2018-02-17 10:00:00'
        ),
        'end' =>  \DateTime::createFromFormat(
            'Y-m-d H:i:s', '2018-02-17 12:00:00'
        ),
    ],
    [
        'start' =>  \DateTime::createFromFormat(
            'Y-m-d H:i:s', '2018-02-17 15:00:00'
        ),
        'end' =>  \DateTime::createFromFormat(
            'Y-m-d H:i:s', '2018-02-17 17:00:00'
        ),
    ],
    [
        'start' =>  \DateTime::createFromFormat(
            'Y-m-d H:i:s', '2018-02-17 21:00:00'
        ),
        'end' =>  \DateTime::createFromFormat(
            'Y-m-d H:i:s', '2018-02-17 22:00:00'
        ),
    ],

    // ...etc
];



$events = [
    [
        'name' => 'even1',
        'duration' => 30,
    ],
    [
        'name' => 'even2',
        'duration' => 210,
    ],
    [
        'name' => 'even3',
        'duration' => 150,
    ],
    [
        'name' => 'even4',
        'duration' => 30,
    ],
    [
        'name' => 'even5',
        'duration' => 120,
    ],
    [
        'name' => 'even5',
        'duration' => 160,
    ],

    // ...etc
];

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-02-17
@Stalker_RED

This is a variation of the containerizing problem .
Knowing what it's called, you can easily find ready-made implementations https://www.google.com/search?q=php+bin+packing+problem

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question