Untitled

mail@pastecode.io avatar
unknown
javascript
7 months ago
586 B
1
Indexable
Never
function findLastBusTime(schedule, time) {
    function timeToMinutes(t) {
        const [h, m] = t.split(":").map(Number);
        return h * 60 + m;
    }

    const currentTime = timeToMinutes(time);
    
    let lastBusTime = -1;
    
    for (let busTime of schedule) {
        const busTimeMinutes = timeToMinutes(busTime);
        if (busTimeMinutes < currentTime) {
            lastBusTime = busTimeMinutes;
        } else {
            break;
        }
    }
    
    if (lastBusTime === -1) {
        return -1;
    } else {
        return currentTime - lastBusTime;
    }
}

Leave a Comment