[LeetCode] Can Place Flowers 可以放置花


 

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die.

Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n, return if n new flowers can be planted in it without violating the no-adjacent-flowers rule.

Example 1:

Input: flowerbed = [1,0,0,0,1], n = 1
Output: True

 

Example 2:

Input: flowerbed = [1,0,0,0,1], n = 2
Output: False

 

Note:

  1. The input array won't violate no-adjacent-flowers rule.
  2. The input array size is in the range of [1, 20000].
  3. n is a non-negative integer which won't exceed the input array size.

 

這道題給了我們一個01數組,其中1表示已經放了花,0表示可以放花的位置,但是有個限制條件是不能有相鄰的花。那么我們來看如果是一些簡單的例子,如果有3個連續的零,000,能放幾盆花呢,其實是要取決約左右的位置的,如果是10001,那么只能放1盆,如果左右是邊界的花,那么就能放兩盆,101,所以如果我們想通過計算連續0的個數,然后直接算出能放花的個數,就必須要對邊界進行處理,處理方法是如果首位置是0,那么前面再加上個0,如果末位置是0,就在最后面再加上個0。這樣處理之后我們就默認連續0的左右兩邊都是1了,這樣如果有k個連續0,那么就可以通過(k-1)/2來快速計算出能放的花的數量,參見代碼如下:

 

解法一:

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        if (flowerbed.empty()) return false;
        if (flowerbed[0] == 0) flowerbed.insert(flowerbed.begin(), 0);
        if (flowerbed.back() == 0) flowerbed.push_back(0);
        int len = flowerbed.size(), cnt = 0, sum = 0;
        for (int i = 0; i <= len; ++i) {
            if (i < len && flowerbed[i] == 0) ++cnt;
            else {
                sum += (cnt - 1) / 2;
                cnt = 0;
            }
        }
        return sum >= n;
    }
};

 

我們也可以直接通過修改flowerbed的值來做,我們遍歷花床,如果某個位置為0,我們就看其前面一個和后面一個位置的值,注意處理首位置和末位置的情況,如果pre和next均為0,那么說明當前位置可以放花,我們修改flowerbed的值,並且n自減1,最后看n是否小於等於0,參見代碼如下:

 

解法二:

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        for (int i = 0; i < flowerbed.size(); ++i) {
            if (n == 0) return true;
            if (flowerbed[i] == 0) {
                int next = (i == flowerbed.size() - 1 ? 0 : flowerbed[i + 1]);
                int pre = (i == 0 ? 0 : flowerbed[i - 1]);
                if (next + pre == 0) {
                    flowerbed[i] = 1;
                    --n;
                }
            }
        }
        return n <= 0;
    }
};

 

下面這種方法跟上面的方法類似,為了不特殊處理首末位置,直接先在首尾各加了一個0,然后就三個三個的來遍歷,如果找到了三個連續的0,那么n自減1,i自增1,這樣相當於i一下向后跨了兩步,可以自行帶例子檢驗,最后還是看n是否小於等於0,參見代碼如下:

 

解法三:

class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        flowerbed.insert(flowerbed.begin(), 0);
        flowerbed.push_back(0);
        for (int i = 1; i < flowerbed.size() - 1; ++i) {
            if (n == 0) return true;
            if (flowerbed[i - 1] + flowerbed[i] + flowerbed[i + 1] == 0) {
                --n;
                ++i;
            }
        }
        return n <= 0;
    }
};

 

類似題目:

Teemo Attacking

 

參考資料:

https://discuss.leetcode.com/topic/91376/simplest-c-code

https://discuss.leetcode.com/topic/91303/java-greedy-solution-o-flowerbed-beats-100

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM