↓快轉到主要內容
  1. LeetCode/

LeetCode 605: Can Place Flowers

·1 分鐘· ·
LeetCode Easy Array Greedy
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: easy 第一次嘗試:2022-08-28

  • 提交紀錄:Runtime Error ×2 → Wrong Answer → Internal Error → Wrong Answer → Accepted → Wrong Answer ×5 → Accepted

解題思路
#

貪心(Greedy):從左往右,只要這格是 0、左邊和右邊都不是 1(兩端外面當成空的),就種下去,並把這格改成 1,這樣下一格就會被擋住。能種越早越好,因為種在比較左邊只會讓右邊的選擇更多。最後比較種下的數量 >= n。

我的嘗試
#

這題 2022 年和 2025 年各寫了一次,總共錯了 10 次:

  • 2022:一開始算「不靠近任何花的空格有幾個」,但新種的花也會擋住旁邊的格子,這樣算會把相鄰的空格都算進去:[0, 0]、n = 2 回傳 True,答案 False。改成在頭尾補 0、種下去就標記才 AC。
  • 2025:用 prev_plant、next_plant 兩個變數追蹤,先是把 prev_plant 打成 pre_plant,左邊永遠被當成空的;修好之後最後一行寫成 plots <= n,比較方向寫反了。

正確解法
#

class Solution:
    def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
        bed = [0] + flowerbed + [0]
        count = 0
        for i in range(1, len(bed) - 1):
            if bed[i - 1] == bed[i] == bed[i + 1] == 0:
                bed[i] = 1
                count += 1
        return count >= n

複雜度
#

時間 O(n),空間 O(n)(加上頭尾的哨兵;直接在原陣列判斷邊界可以 O(1))。

要特別避免的錯誤
#

  • 種下去之後要更新陣列,不然相鄰的兩個空格都會被算成可以種。
  • 比較方向是 count >= n。

面試口說整理
#

我在頭尾各補一個空格當哨兵,從左往右掃,左右都是空的就種下去並標記,種越早越好,最後看種下的數量是否不少於 n。時間 O(n)。

相關文章

LeetCode 561: Array Partition
·1 分鐘
LeetCode Easy Array Greedy Sorting Counting-Sort
LeetCode 561 解題筆記:排序後相鄰兩個一組,每組的最小值就是偶數位置的數,加總
LeetCode 455: Assign Cookies
·1 分鐘
LeetCode Easy Array Two-Pointers Greedy Sorting
LeetCode 455 解題筆記:小孩和餅乾都排序,用最小的、夠大的餅乾滿足最小的小孩;兩個指標各走一次
LeetCode 598: Range Addition II
·1 分鐘
LeetCode Easy Array Math
LeetCode 598 解題筆記:每次操作都包含左上角,被加最多次的是所有操作的交集:min(a) × min(b)
LeetCode 599: Minimum Index Sum of Two Lists
·1 分鐘
LeetCode Easy Array Hash-Table String
LeetCode 599 解題筆記:把 list1 的字串對應到 index,掃 list2 時算兩邊 index 的和,留下和最小的全部字串
LeetCode 566: Reshape the Matrix
·1 分鐘
LeetCode Easy Array Matrix Simulation
LeetCode 566 解題筆記:元素總數不同就回傳原矩陣;否則第 k 個元素放到 (k // c, k % c)
LeetCode 575: Distribute Candies
·1 分鐘
LeetCode Easy Array Hash-Table
LeetCode 575 解題筆記:最多只能吃 n / 2 顆,種類最多 len(set),答案是兩者的較小值