test case is wrong i guess


  • 0
    J
    int kEmptySlots(vector<int>& flowers, int k) {
        int n = flowers.size();
        vector<int> flowerPositions(flowers.size(), -1);
        for(int i =0; i<flowers.size(); ++i) {
            flowerPositions[flowers[i]-1] = i;
        }
        deque<int> minQue;
        for(int i=0; i<flowers.size(); ++i) {
            if(minQue.empty())  {
                minQue.push_back(i);
            }else {
                if(i-minQue.front()>=k) {
                    if(!minQue.empty()) minQue.pop_front();
                }
                while( (!minQue.empty()) && (flowerPositions[minQue.back()] > flowerPositions[i] ) ) {
                    minQue.pop_back();
                }
                minQue.push_back(i);
            }
            if( (i>=k) && (i<n-1) ) {
                int minTime = flowerPositions[minQue.front()];
                int maxTime = max(flowerPositions[i-k], flowerPositions[i+1]);
                if( (minTime>maxTime) ) {
                    return maxTime+1;
                }
            }      
        }        
        return -1;
    
    }
    

    [3,9,2,8,1,6,10,5,4,7]
    1
    my answer 8
    leeteocde 6
    but my answer seems work too


  • 0
    J
    This post is deleted!

Log in to reply
 

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