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
.flowerbed
.0 <= n <= flowerbed.length
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
F F
! 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
temp.f95:15:12: 15 | program main | 1 Error: Unexpected PROGRAM statement at (1) temp.f95:25:16: 25 | end program main | 1 Error: Syntax error in END PROGRAM statement at (1) temp.f95:30:51: 30 | integer, dimension(:), allocatable :: flowerbed | 1 Error: Symbol βflowerbedβ at (1) already has basic type of INTEGER temp.f95:31:16: 31 | integer :: n | 1 Error: Symbol βnβ at (1) already has basic type of INTEGER temp.f95:34:24: 34 | logical :: can_plant | 1 Error: Unexpected data declaration statement at (1) temp.f95:37:16: 37 | integer :: i, j, count | 1 Error: Symbol βiβ at (1) already has basic type of INTEGER temp.f95:79:3: 79 | end function can_plant_flowers | 1 Error: Expecting END PROGRAM statement at (1) f951: Error: Unexpected end of file in βtemp.f95β
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.
i
.flowerbed[i]
is already 0,i
is 0 (first plot) or the plot before the current plot is empty,i
is the last plot or the plot after the current plot is empty.flowerbed[i]
to 1 and increment the count.n
, return true.n
, return true; otherwise, return false.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.
i
.flowerbed[i]
is already 0,i
is 0 (first plot) or the plot before the current plot is empty,i
is the last plot or the plot after the current plot is empty.flowerbed[i]
to 1 and increment the count.n
, return true.n
, return true; otherwise, return false.