Similar Problems

Similar Problems not available

Earliest Possible Day Of Full Bloom - Leetcode Solution

Companies:

LeetCode:  Earliest Possible Day Of Full Bloom Leetcode Solution

Difficulty: Hard

Topics: greedy sorting array  

The Earliest Possible Day Of Full Bloom problem on LeetCode is a problem that deals with determining the earliest possible day when a flower bed can be in full bloom. This problem is a simulation problem that involves using binary search to determine the earliest day for a given set of conditions.

The problem statement is as follows:

You have a flower bed of size n containing several different flowers, represented by an array flowers where flowers[i] is the type of flower in the ith position. You want to create new flowers in your flower bed using these flowers. Your task is to determine the earliest day that can be chosen to create new flowers so that each position in the flower bed will be in full bloom.

A position is said to be in full bloom if:

  • No adjacent position is empty.
  • Each of the positions adjacent to the current position is blooming as well.

For example, if we have a flower bed containing the following flowers: [1,0,0,0,0,1], then we can create new flowers on the 3rd and 5th positions on the flower bed, resulting in the following flower bed: [1,0,1,0,1,1]. Each position is in full bloom on the 5th day.

To solve this problem, we can use binary search to determine the earliest possible day for full bloom. We first define the range of possible days for full bloom to occur. Here, the range is between the earliest day (1) and the latest day (10^9).

We then use binary search to determine the earliest day that can be chosen to create new flowers. In each binary search iteration, we check if the current day is a valid choice for creating new flowers. We do this by iterating through the flower bed array, checking if a position is empty and if its adjacent positions are already blooming. If a position is empty and its adjacent positions are already blooming, we consider creating a new flower in that position. We continue this process until we have created all the necessary flowers for the flower bed to be in full bloom. If we cannot create all the necessary flowers with the current day, we move to the right half of the search range. Otherwise, we move to the left half of the search range.

At the end of the binary search, we return the earliest possible day for full bloom. If no such day exists, we return -1.

Here is the step-by-step solution to the Earliest Possible Day Of Full Bloom problem on LeetCode:

Step 1: Define the search range

We define the search range as the earliest day (1) and the latest day (10^9).

Step 2: Implement the binary search

We implement the binary search using two pointers (left and right). We first set the left pointer to the earliest day (1) and the right pointer to the latest day (10^9).

While the left pointer is less than or equal to the right pointer, we calculate the middle day as follows:

int mid = left + (right - left) / 2;

We then check if the current day is a valid choice for creating new flowers. We use a boolean variable called validDay to keep track of the validity of the current day. We initialize validDay to false.

for (int i = 0; i < n; i++) { if (flowers[i] == 0) { if ((i == 0 || flowers[i-1] < mid) && (i == n-1 || flowers[i+1] < mid)) { flowers[i] = mid; validDay = true; k--; if (k == 0) break; } } }

In the above code, we iterate through the flower bed array. For each empty position (flowers[i] == 0), we check if its adjacent positions are already blooming. If they are, we consider creating a new flower in that position (flowers[i] = mid). We also decrement k, which represents the number of flowers we need to create for the flower bed to be in full bloom. If k reaches 0, we break out of the loop.

If we can create all the necessary flowers with the current day, we move to the left half of the search range (right = mid - 1). Otherwise, we move to the right half of the search range (left = mid + 1). We also reset the flower bed array to its original state.

if (validDay) { res = mid; right = mid - 1; } else { left = mid + 1; } std::copy(origFlowers, origFlowers + n, flowers);

At the end of the binary search, we return res, which represents the earliest possible day for full bloom. If no such day exists, we return -1.

Full Code:

int kEmptySlots(vector<int>& flowers, int k) { int n = flowers.size(); int origFlowers[n]; std::copy(flowers.begin(), flowers.end(), origFlowers); int left = 1, right = 1e9, res = -1; while (left <= right) { int mid = left + (right - left) / 2; bool validDay = false; for (int i = 0; i < n; i++) { if (flowers[i] == 0) { if ((i == 0 || flowers[i-1] < mid) && (i == n-1 || flowers[i+1] < mid)) { flowers[i] = mid; validDay = true; k--; if (k == 0) break; } } } if (validDay) { res = mid; right = mid - 1; } else { left = mid + 1; } std::copy(origFlowers, origFlowers + n, flowers); } return res; }

Earliest Possible Day Of Full Bloom Solution Code

1