AAkifhanIlgazadd js
5b4233b6创建于 2023年2月8日历史提交
/**
 * @param {number[]} fruits
 * @return {number}
 */
var totalFruit = function (fruits) {
    let count = new Map();
    let [left, total, res] = [0, 0, 0];

    for (fruit of fruits) {
        count.set(fruit, (count.get(fruit) || 0) + 1);
        total++;

        while (count.size > 2) {
            let f = fruits[left];
            count.set(f, count.get(f) - 1);
            total -= 1;
            left += 1;
            if (!count.get(f)) {
                count.delete(f);
            }
        }
        res = Math.max(res, total);
    }

    return res;
};