Planting flowers with no adjacent flower plots

Problem

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 booleans), return if a given number of new flowers can be planted in it without violating the no-adjacent-flowers rule 
Sample inputs 

Input: 1,0,0,0,0,0,1,0,0 

3 => true 
4 => false 
Input: 1,0,0,1,0,0,1,0,0 

1 => true 
2 => false 
input: 0 

1 => true 
2 => false 

public boolean canPlaceFlowers(List flowerbed, int numberToPlace) { 

// Implementation here 
}

Answer

Brute-force approach, $O(N \times N!)$


   The first idea that came to my mind after understanding this question was to use DFS to find the best possible way to plant the flowers.
  Using DFS with the backtracking, we can easily determine whether a given target number of flowers can be planted. However, this will potentially take $O(N \times N!)$.

This is way too slow, which led me to the next solution, which I was able to complete in 32 mins.


Dynamic programming, O(N)

 In dynamic programming, we need to:

  • 1. image the ideal solution, $S_{n}$, where n is the last pot in the flowerbed.
    • Given the restriction of consecutive planting, the possible max number of pots $S_{n}$ can be written as below: $$S_{n} = MAX(S_{n-1}, S_{n-2} + W_{n}$$ where $W_{n}$ is 1 if n th pot is plantable, - $\infty$.
  • 2. build up the loop from the beginning based on the ideal solution by filling in the $S_{i}$ following the formula below: $$S_{i} = MAX(S_{i-1}, S_{i-2} + W_{i}$$
  • 3. backtrace the solution by walking the array S if necessary

Linear search with preprocessing ( not working)


I could not come up with a better name so I stated merely what the algorithm does.
Instead of using DFS, we can preprocess to identify the available empty plot in the flowerbed.

Figure1. Example algorithm
Step 1: 
  As you can see in Figure 1, we first mark the all unavailable plot in the flower bed.  Any plots right before and after the occupied plot (marked as 1) are marked as 1.

Step 2:
   We now compile the list of consecutive empty plots (a cluster) by scanning the list.

Step 3:
    We now scan the list of clusters of empty slots and calculate the number of flowers to plants as below:
              Maximum number of flows to plants, M = $\lceil \text{number of empty plots} \rceil$

 We can continue to subtract the M from the target count while scanning the list of clusters.
This algorithm will take $O (N)$, where N is the length of the floor bed.

UPDATE(2022-06-28): After reviewing the original algorithm, I found that the original algorithm with the preprocessing has a bug. If empty slots after pre-processing are consecutive, we can't use the count as they are. We still need to find out how many plant we can plant for the consecutive slots.

Greedy algorithm approach


    The last way to solve the problem is to use a greedy algorithm. We try to plant the flower in the first available empty slot and continue to plant until there are no more available plots.
Surprisingly, this approach works in this case without causing the problem (draw it on the paper to see if it is true.)

This makes the code simpler than the previous approach with the same time complexity $O(N)$.


Here is the complete code for the greedy algorithm.
Here is the code for the linear search with preprocessing


UPDATE (2022-06-28): Solved the problem with the brute-force DFS. It is not optimal. 


18:45: for algorithm and bug-free code

I tried to do it with dynamic programming. This can run in $O(N)$ time. Much faster.

18:00 to write up the code.

Comments

Popular posts from this blog

Find the maximum number of bomb that can be detonated