2251. Number of Flowers in Full Bloom - Explanation

Problem Link



1. Brute Force

class Solution:
    def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]:
        res = []

        for time in people:
            cnt = 0
            for start, end in flowers:
                if start <= time <= end:
                    cnt += 1
            res.append(cnt)

        return res

Time & Space Complexity

  • Time complexity: O(mn)O(m * n)
  • Space complexity: O(m)O(m) for the output array.

Where nn is the size of the array flowersflowers, and mm is the size of the array peoplepeople.


2. Two Min-Heaps

class Solution:
    def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]:
        people = sorted((p, i) for i, p in enumerate(people))
        res = [0] * len(people)
        count = 0

        start = [f[0] for f in flowers]
        end = [f[1] for f in flowers]

        heapq.heapify(start)
        heapq.heapify(end)

        for p, i in people:
            while start and start[0] <= p:
                heapq.heappop(start)
                count += 1
            while end and end[0] < p:
                heapq.heappop(end)
                count -= 1
            res[i] = count

        return res

Time & Space Complexity

  • Time complexity: O(mlogm+nlogn)O(m \log m + n \log n)
  • Space complexity: O(m+n)O(m + n)

Where nn is the size of the array flowersflowers, and mm is the size of the array peoplepeople.


3. Min-Heap

class Solution:
    def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]:
        people = sorted((p, i) for i, p in enumerate(people))
        res = [0] * len(people)
        flowers.sort()
        end = []

        j = 0
        for p, i in people:
            while j < len(flowers) and flowers[j][0] <= p:
                heapq.heappush(end, flowers[j][1])
                j += 1
            while end and end[0] < p:
                heapq.heappop(end)
            res[i] = len(end)

        return res

Time & Space Complexity

  • Time complexity: O(mlogm+nlogn)O(m \log m + n \log n)
  • Space complexity: O(m+n)O(m + n)

Where nn is the size of the array flowersflowers, and mm is the size of the array peoplepeople.


4. Sorting + Two Pointers

class Solution:
    def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]:
        start = sorted(f[0] for f in flowers)
        end = sorted(f[1] for f in flowers)

        res = [0] * len(people)
        peopleIndex = sorted((p, i) for i, p in enumerate(people))

        i = j = count = 0
        for p, index in peopleIndex:
            while i < len(start) and start[i] <= p:
                count += 1
                i += 1
            while j < len(end) and end[j] < p:
                count -= 1
                j += 1
            res[index] = count

        return res

Time & Space Complexity

  • Time complexity: O(mlogm+nlogn)O(m \log m + n \log n)
  • Space complexity: O(m+n)O(m + n)

Where nn is the size of the array flowersflowers, and mm is the size of the array peoplepeople.


5. Line Sweep

class Solution:
    def fullBloomFlowers(self, flowers: List[List[int]], people: List[int]) -> List[int]:
        events = []
        for start, end in flowers:
            events.append((start, 1))
            events.append((end + 1, -1))

        events.sort()
        queries = sorted((p, i) for i, p in enumerate(people))
        res = [0] * len(people)

        count = j = 0
        for time, index in queries:
            while j < len(events) and events[j][0] <= time:
                count += events[j][1]
                j += 1
            res[index] = count

        return res

Time & Space Complexity

  • Time complexity: O(mlogm+nlogn)O(m \log m + n \log n)
  • Space complexity: O(m+n)O(m + n)

Where nn is the size of the array flowersflowers, and mm is the size of the array peoplepeople.