Similar Problems

Similar Problems not available

Number Of Flowers In Full Bloom - Leetcode Solution

LeetCode:  Number Of Flowers In Full Bloom Leetcode Solution

Difficulty: Hard

Topics: hash-table binary-search array prefix-sum sorting  

Problem Statement:

You have N flowers numbered 1 to N. Each flower blooms in exactly one season, but some seasons can see multiple flowers blooming together. The flowers are arranged in a line, and each flower is described by an integer i, the season in which it blooms. We call three flowers i, j, and k, a triplet if i<j<k. In other words, i is the bloom season of the first flower, j is the bloom season of some second flower placed exactly to the right of the first, and k is the bloom season of some third flower placed exactly to the right of the second flower, such that j-i = k-j. Note that j-i = k-j should be considered as one constraint and not as two separate constraints.

Given the bloom seasons of all N flowers, find the number of triplets of blooming flowers.

Example 1: Input : blooms = [1,3,1,4,2,3,4,2], k = 2 Output : 4 Explanation: The first three flowers form a triplet (1, 3, 4). The next three flowers form another triplet (1, 2, 3). The fourth, fifth, and sixth flowers form another triplet (2, 3, 4). The fifth, sixth, and seventh flowers form the last triplet (2, 4, 4). In total, there are 4 triplets of blooming flowers.

Example 2: Input: blooms = [1,1,2,2,3,3,4,4], k = 1 Output: 0

Approach:

We can solve this problem easily using a sliding window approach. Here’s how we can solve this problem:

  1. We define our window size to be k, i.e., the difference between j and i should be ≤ k.

  2. We keep a window of size k and slide it from left to right until we reach the end of the array. For each window, we count the number of triplets.

  3. For every window, we can check the presence of triplets in O(k) time by iterating over all possible pairs of j and i between the start and end of the window, and checking if there exists a third integer k such that j-i = k-j. If such an integer k exists, then the triplet (i, j, k) is formed.

  4. We can improve the above step by maintaining two arrays. The first array, min_left[i] stores the minimum value of blooms[j] for all indicies j ≤ i-k. The second array max_right[i] stores the maximum value of blooms[j] for all indicies j ≥ i.

  5. Then for every window, we check if the value of the current index (j) is the maximum value in the current window and also the minimum value in all windows that contain it (i.e., in the range i-k+1∼j). If both these conditions are true, then the jth flower can form a triplet with any other two flowers in the current window, and there can be (j-i-k+1) such triplets.

Implementation:

let’s solve the above problem step-by-step by following the above approach.

  1. We initialize our arrays min_left and max_right. We can do this in O(N) time.

min_left = [0]*N max_right = [0]*N

min_left[0] = blooms[0] for i in range(1, N): min_left[i] = min(min_left[i-1], blooms[i])

max_right[N-1] = blooms[N-1] for i in range(N-2, -1, -1): max_right[i] = max(max_right[i+1], blooms[i])

  1. We then iterate over all possible windows of size k and check if any triplets can be formed for each window.

result = 0 for i in range(N-k+1): # Loop over all windows of size k. window_min = min_left[i+k-1] # Find the minimum value in the current window. window_max = max_right[i] # Find the maximum value in the current window.

if i > 0:  # Check if there's any value less than the minimum in the left subarray.
    left_min = min_left[i-1]
    if left_min < window_min:
        continue

if i+k < N:  # Check if there's any value greater than the maximum in the right subarray.
    right_max = max_right[i+k]
    if right_max > window_max:
        continue

for j in range(i+k-1, N):  # Loop over all possible pairs.
    if blooms[j] == window_max and blooms[i+k-1] == window_min:
        result += (j-i-k+2)//k  # If triplet is formed, then add the number of triplets to result.

3. Finally, we return the result.

return result

Time Complexity:

The complexity of the above approach is O(N), where N is the number of flowers. We do a constant amount of work for each of the N windows of size k. The work inside each window takes O(k) time, but since k is constant, this time can be considered constant as well. Therefore, the overall time complexity is O(N).

Space Complexity:

The space complexity of the above approach is O(N), where N is the number of flowers. We need two extra arrays of size N to store the minimum and maximum values encountered so far.

Number Of Flowers In Full Bloom Solution Code

1