881. The lifeboat

They limit each boat to two people, so you can be greedy. Rank people, and compare the combination of the leanest and the fattest with the limit. If it is larger than the limit, the fattest needs to take a boat alone, and then compare the second fattest with the leanest.

var numRescueBoats = function(people, limit) {
    var count = 0
    let thin = 0,
        fat = people.length - 1;
    people.sort((a, b) = > a - b)
    while (thin <= fat) {
        if (people[thin] + people[fat] <= limit) {
            thin++
        }
        fat--
        count++
    }
    return count
};
Copy the code

Sword refers to Offer 52. The first common node of two linked lists

A hash set stores a linked list and iterates through another linked list to see if the node exists in the hash table.

var getIntersectionNode = function(headA, headB) {
    const visited = new Set(a);let temp = headA;
    while(temp ! = =null) {
        visited.add(temp);
        temp = temp.next;
    }
    temp = headB;
    while(temp ! = =null) {
        if (visited.has(temp)) {
            return temp;
        }
        temp = temp.next;
    }
    return null;
};
Copy the code

The end condition is that the linked list intersects for the first time or both lists have been traversed.

var getIntersectionNode = function(headA, headB) {
    if (headA === null || headB === null) {
        return null;
    }
    let pA = headA, pB = headB;
    while(pA ! == pB) { pA = pA ===null ? headB : pA.next;
        pB = pB === null ? headA : pB.next;
    }
    return pA;
};
Copy the code