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 <= flowerbed.length <= 2 * 104
  • flowerbed[i] is 0 or 1.
  • There are no two adjacent flowers in flowerbed.
  • 0 <= n <= flowerbed.length

Note: This problem is from LeetCode.
βœ… Compiled
βœ… Executed
⚠️ Correct
program main
    implicit none

    integer, parameter :: n = 5
    integer, dimension(n) :: flowerbed
    logical :: result

    flowerbed = [1, 0, 0, 0, 1]
    result = canPlaceFlowers(flowerbed, 1)
    print '(L1)', result

    flowerbed = [1, 0, 0, 0, 1]
    result = canPlaceFlowers(flowerbed, 2)
    print '(L1)', result

contains

    function canPlaceFlowers(flowerbed, n) result(canPlace)
        implicit none
        integer, intent(in) :: flowerbed(:), n
        logical :: canPlace
        integer :: i, j

        do i = 1, size(flowerbed) - 1
            if (flowerbed(i) == 0 .and. flowerbed(i+1) == 0) then
                do j = i + 1, i + n
                    if (j > size(flowerbed)) exit
                    if (flowerbed(j) /= 0) then
                        canPlace = .false.
                        exit
                    end if
                end do
                if (canPlace) exit
            end if
        end do

        if (i > size(flowerbed)) then
            canPlace = .true.
        else
            canPlace = .false.
        end if

    end function canPlaceFlowers
end program main
❌ Compiled
❌ Executed
❌ Correct
! This is a full Fortran script with a valid main entry point that solves the problem
! described in the statement.

! Declare the input variables
integer, dimension(:), allocatable :: flowerbed
integer :: n

! Declare the output variable
logical :: can_plant_flowers

! Declare the temporary variables
integer :: i, j, count

! Main entry point
program main
    ! Read the input from the standard input
    read(*,*) flowerbed
    read(*,*) n

    ! Call the function to check if flowers can be planted
    can_plant_flowers = can_plant_flowers(flowerbed, n)

    ! Print the output to the standard output
    write(*,*) can_plant_flowers
end program main

! Function to check if flowers can be planted
function can_plant_flowers(flowerbed, n) result(can_plant)
    ! Declare the input variables
    integer, dimension(:), allocatable :: flowerbed
    integer :: n

    ! Declare the output variable
    logical :: can_plant

    ! Declare the temporary variables
    integer :: i, j, count

    ! Initialize the output variable
    can_plant = .true.

    ! Check if the input array is valid
    if (size(flowerbed) == 0) then
        return
    end if

    ! Initialize the count of adjacent flowers
    count = 0

    ! Loop through the array and check if flowers can be planted
    do i = 1, size(flowerbed) - 1
        ! Check if the current plot is empty
        if (flowerbed(i) == 0) then
            ! Check if the previous plot is not empty
            if (flowerbed(i - 1) == 1) then
                ! Increment the count of adjacent flowers
                count = count + 1
            end if

            ! Check if the next plot is not empty
            if (flowerbed(i + 1) == 1) then
                ! Increment the count of adjacent flowers
                count = count + 1
            end if

            ! Check if the count of adjacent flowers exceeds the input parameter
            if (count > n) then
                ! Set the output variable to false
                can_plant = .false.

                ! Exit the loop
                exit
            end if
        end if
    end do

    ! Return the output variable
    return
end function can_plant_flowers
🌐 Data from online sources
def canPlaceFlowers(flowerbed, n):
    count = 0
    for i in range(len(flowerbed)):
        if flowerbed[i] == 0 and (i == 0 or flowerbed[i - 1] == 0) and (i == len(flowerbed) - 1 or flowerbed[i + 1] == 0):
            flowerbed[i] = 1
            count += 1
        if count >= n:
            return True
    return count >= n
1. Initialize a variable `count` to keep track of the number of flowers planted.
  1. Iterate through the flowerbed with a variable i.
  2. Check the possibility of planting flowers at the current index:
  3. If flowerbed[i] is already 0,
  4. i is 0 (first plot) or the plot before the current plot is empty,
  5. i is the last plot or the plot after the current plot is empty.
  6. If all these conditions are met, plant a flower at the current plot by setting flowerbed[i] to 1 and increment the count.
  7. If the count is equal to or greater than n, return true.
  8. After iterating through the entire array, if the count is still equal to or greater than n, return true; otherwise, return false.
🌐 Data from online sources
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
    int count = 0;
    for(int i = 0; i < flowerbed.size(); i++) {
        if(flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == flowerbed.size() - 1 || flowerbed[i + 1] == 0)) {
            flowerbed[i] = 1;
            count++;
        }
        if(count >= n) return true;
    }
    return count >= n;
}
1. Initialize a variable `count` to keep track of the number of flowers planted.
  1. Iterate through the flowerbed with a variable i.
  2. Check the possibility of planting flowers at the current index:
  3. If flowerbed[i] is already 0,
  4. i is 0 (first plot) or the plot before the current plot is empty,
  5. i is the last plot or the plot after the current plot is empty.
  6. If all these conditions are met, plant a flower at the current plot by setting flowerbed[i] to 1 and increment the count.
  7. If the count is equal to or greater than n, return true.
  8. After iterating through the entire array, if the count is still equal to or greater than n, return true; otherwise, return false.