683. K Empty Slots解題報(bào)告

Description:

There is a garden with N slots. In each slot, there is a flower. The N flowers will bloom one by one in N days. In each day, there will be exactly one flower blooming and it will be in the status of blooming since then.

Given an array flowers consists of number from 1 to N. Each number in the array represents the place where the flower will open in that day.

For example, flowers[i] = x means that the unique flower that blooms at day i will be at position x, where i and x will be in the range from 1 to N.

Also given an integer k, you need to output in which day there exists two flowers in the status of blooming, and also the number of flowers between them is k and these flowers are not blooming.

If there isn't such day, output -1.

Example:

Example 1:

Input: 
flowers: [1,3,2]
k: 1
Output: 2
Explanation: In the second day, the first and the third flower have become blooming.

Example 2:

Input: 
flowers: [1,2,3]
k: 1
Output: -1

Link:

https://leetcode.com/problems/k-empty-slots/description/

解題方法:

這道題是求在某一天(某次開(kāi)花后)是否有一個(gè)以左右兩朵花為邊界,中間部分不開(kāi)花,并且中間部分長(zhǎng)度為k的區(qū)域。

暴力破解就是按照數(shù)組順序每次迭代開(kāi)一朵花,記錄開(kāi)花位置,每次迭代都掃一遍數(shù)組看看有沒(méi)有符合以上的一段區(qū)域。這樣的時(shí)間復(fù)雜度是O(N^2);

假設(shè)把整個(gè)數(shù)組當(dāng)成一個(gè)大區(qū)域(0到size - 1),以-1和size為邊界;把每次開(kāi)花都當(dāng)成用刀切割當(dāng)前區(qū)域,如果在某次切割后(某日開(kāi)花后)能出現(xiàn)符合上面條件的區(qū)域,那么就找到了這樣一個(gè)天數(shù)。
如果這樣理解,這道題就可以轉(zhuǎn)化為二叉樹(shù)的題來(lái)考慮,如果這道題只是求到這么一個(gè)天數(shù),用DFS就可以做出來(lái)。如果需要找到最早的天數(shù),用BFS也可以做出來(lái)。

Tips:

這道題在LeetCode上是要找到最短天數(shù),所以用BFS。

Time Complexity:

完整代碼:

// DFS:
int kEmptySlots(vector<int>& flowers, int k) {
    int size = flowers.size();
    if(size < 2 || size - 2 < k)
        return -1;
    int result = -1;
    helper(flowers, k, -1, -1, size, result);
    return result;
}
void helper(vector<int>& flowers, int k, int day, int lB, int rB, int& result) {
    if(rB - lB < k + 1 || result != -1 || day >= (int) flowers.size()) {
        return;
    }
    if(rB - lB == k + 1 && lB != -1 && rB != (int) flowers.size()) {
        result = day + 1;
        return;
    }
    int curr = flowers[day + 1] - 1;
    if(curr <= lB || curr >= rB)
        helper(flowers, k, day + 1, lB, rB, result);
    else {
        helper(flowers, k, day + 1, lB, curr, result);
        helper(flowers, k, day + 1, curr, rB, result);
    }
}

// BFS(AC方法):
int kEmptySlots(vector<int>& flowers, int k) {
    int size = flowers.size();
    if(size < 2 || size - 2 < k || k < 0)
        return -1;
    int day = 0;
    queue<pair<int, int>> Q;
    Q.push(pair<int, int> (-1, size));
    while(!Q.empty()) {
        int len = Q.size();
        while(len-- > 0) {
            int l = Q.front().first, r = Q.front().second;
            Q.pop();
            if(r - l < k + 1)
                continue;
            if(r - l == k + 1 && l != -1 && r != size)
                return day;
            int curr = flowers[day] - 1;
            if(curr <= l || curr >= r)
                Q.push(pair<int, int> (l, r));
            else {
                Q.push(pair<int, int> (l, curr));
                Q.push(pair<int, int> (curr, r));
            }

        }
        ++day;
    }
    return -1;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,847評(píng)論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,064評(píng)論 0 23
  • 綿延心底的云煙暮靄,連同隔著時(shí)空的一箋眷念,終會(huì)隨著季節(jié)的更迭而削減,只是偶爾夢(mèng)里又見(jiàn)故人來(lái)。曾經(jīng)的故事擦肩...
    那些年聆聽(tīng)的閱讀 225評(píng)論 0 0
  • 晚風(fēng)從田野奔向河畔 化成月影在水間變幻 波光一片搖搖 長(zhǎng)夜也可謂漫漫 高樓見(jiàn)不到流螢 星辰模糊不清 睡意微微 輾轉(zhuǎn)
    晴柒陌沫閱讀 402評(píng)論 0 2
  • 一直想活成這樣子,奈何……
    糖果_bf9b閱讀 165評(píng)論 0 0

友情鏈接更多精彩內(nèi)容