LeetCode 1375 Javascript

152 阅读1分钟

超级慢

但是代码逻辑清晰易懂

呵呵哒

/**
 * @param {number[]} light
 * @return {number}
 */
var numTimesAllBlue = function(light) {
    let len = light.length;
    let states = [];
    let max = -1;
    let counter = 1;
    let smoothMax = 0;
    for (let i = 0; i < len - 1; ++i) {
        let index = light[i];
        states[index] = true;
        max = Math.max(max, index);
        let flag = true;
        for (let j = smoothMax || 1; j <= max; ++j) {
            if (!states[j]) {
                smoothMax = j - 1;
                flag = false;
                break;
            }
        }
        if (flag) {
            counter ++;
        }
    }
    return counter;
};