JavaScript Solution


  • 0
    S

    I don't see any JavaScript solutions here in discussion, so just put mine here:

    var kEmptySlots = function(flowers, k) {
        var theDay = -1;
        var gardenSlot = [];
        var days = flowers.length;
        var location, verify;
        gardenSlot[flowers[0] - 1] = 1;
        for (var i = 1; i < days; i++) {
            if (flowers[i] - 1 - k - 1 >= 0 && gardenSlot[flowers[i] - 1 - k - 1] === 1) {
                location = flowers[i] - 1 - k - 1;
                verify = true;
                for (var j = 1; j < k + 1; j++) {
                    if (gardenSlot[location + j] === 1) {
                        verify = false;
                        break;
                    }
                }
                if (verify) {
                    return i + 1;
                }
            } else if (flowers[i] - 1 + k + 1 <= days - 1 && gardenSlot[flowers[i] - 1 + k + 1] === 1) {
                location = flowers[i] - 1 + k + 1;
                verify = true;
                for (var j = 1; j < k + 1; j++) {
                    if (gardenSlot[location - j] === 1) {
                        verify = false;
                        break;
                    }
                }
                if (verify) {
                    return i + 1;
                }
            }
            gardenSlot[flowers[i] - 1] = 1;
        }
        return theDay;
    };
    

    It is currently the fastest solution showed in submission panel, but should not be the fastest solution, since so few people are using JavaScript.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.