>  기사  >  웹 프론트엔드  >  두 포인터 - Javascript를 사용한 공통 시간 슬롯 알고리즘

두 포인터 - Javascript를 사용한 공통 시간 슬롯 알고리즘

PHPz
PHPz원래의
2024-08-30 18:31:451072검색

Two Pointer - Common Time Slot Algorithm Using Javascript

두 사람이 공통의 자리를 찾는 데 문제가 있을 때.

2점식 기술을 사용하여 알아볼 수 있습니다.

function availableDuration(slots1, slots2, d) {
    let i = 0;
    let j = 0;

    while (i < slots1.length && j < slots2.length) {
        // Finding the boundaries of the intersection, or the common slot
        const left = Math.max(slots1[i][0], slots2[j][0]);
        const right = Math.min(slots1[i][1], slots2[j][1]);
        if (right - left >= d) {
            return [left, left + d];
        }
        // Always move the pointer of the slot that ends earlier
        if (slots1[i][1] < slots2[j][1]) {
            i++;
        } else {
            j++;
        }
    }
    return [];
}

// Example usage
const slots1 = [[10, 50], [60, 120], [140, 210]];
const slots2 = [[0, 15], [60, 70]];
const d = 8;
const result = availableDuration(slots1, slots2, d);
console.log("Earliest common time slot:", result);

// Earliest common time slot: [ 60, 68 ]

참고자료

자세한 내용은 확인해주세요
https://www.geeksforgeeks.org/meeting-scheduler-for-two-persons/

위 내용은 두 포인터 - Javascript를 사용한 공통 시간 슬롯 알고리즘의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.