LeetCode //C - 605. Can Place Flowers

605. Can Place Flowers

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.

Given an integer array flowerbed containing 0’s and 1’s, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.
 

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

Constraints:
  • 1 < = f l o w e r b e d . l e n g t h < = 2 ∗ 1 0 4 1 <= flowerbed.length <= 2 * 10^4 1<=flowerbed.length<=2104
  • flowerbed[i] is 0 or 1.
  • There are no two adjacent flowers in flowerbed.
  • 0 <= n <= flowerbed.length

From: LeetCode
Link: 605. Can Place Flowers


Solution:

Ideas:
  • We iterate over each plot in the flowerbed.
  • We check if the current plot is empty and its adjacent plots are also empty (or the plot is at the boundary of the flowerbed).
  • If we can plant a flower, we set flowerbed[i] to 1 and decrement n.
  • After the loop, we check if n has reached 0, indicating that all required flowers have been planted.
Code:
bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n) {
   
    for (int i = 0; i < flowerbedSize && n > 0; i++) {
   
        // Check if current plot is empty and adjacent plots are also empty (or non-existent)
        if (flowerbed[i] == 0) {
   
            bool emptyLeft = (i == 0) || (flowerbed[i - 1] == 0);
            bool emptyRight = (i == flowerbedSize - 1) || (flowerbed[i + 1] == 0);

            if (emptyLeft && emptyRight) {
   
                flowerbed[i] = 1; // Plant a flower
                n--; // Decrease the number of flowers needed
            }
        }
    }
    return n == 0; // Return true if all flowers can be planted, otherwise false
}

相关推荐

  1. LEETCODE605.种花问题

    2023-12-15 08:48:03       54 阅读
  2. leetcode605-Can Place Flowers

    2023-12-15 08:48:03       29 阅读
  3. Leetcode 605. Can Place Flowers

    2023-12-15 08:48:03       32 阅读
  4. LeetCode //C - 605. Can Place Flowers

    2023-12-15 08:48:03       60 阅读
  5. 【LeetCode】607. 销售员

    2023-12-15 08:48:03       62 阅读
  6. springboot615基于springboot的旅游出行指南_655ms--论文

    2023-12-15 08:48:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-15 08:48:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-15 08:48:03       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-15 08:48:03       82 阅读
  4. Python语言-面向对象

    2023-12-15 08:48:03       91 阅读

热门阅读

  1. kafka3.X集群安装(不使用zookeeper)

    2023-12-15 08:48:03       52 阅读
  2. vue-quill-editor上传图片base64转化为img标签

    2023-12-15 08:48:03       61 阅读
  3. ubuntu-base 20.04防火墙配置方法

    2023-12-15 08:48:03       59 阅读
  4. 解释 Git 的基本概念和使用方式

    2023-12-15 08:48:03       58 阅读
  5. Webservice--HTTP,SOAP协议区别

    2023-12-15 08:48:03       45 阅读
  6. redis键值相关命令整理

    2023-12-15 08:48:03       61 阅读
  7. Redis篇

    Redis篇

    2023-12-15 08:48:03      52 阅读
  8. Mistral AI 为什么这么火

    2023-12-15 08:48:03       55 阅读