🧠
Brute Force (Check Each Day Sequentially)
💡 This approach tries every possible day from the earliest bloom to the latest bloom to check if we can make the bouquets. It is simple and intuitive but inefficient for large inputs.
Intuition
We simulate each day starting from the earliest bloom day and check if enough bouquets can be made by counting consecutive bloomed flowers.
Algorithm
- Find the minimum and maximum bloom days.
- For each day from minimum to maximum:
- Check if we can make m bouquets by counting k consecutive bloomed flowers.
- Return the first day where making m bouquets is possible; otherwise, return -1.
💡 The main difficulty is understanding how to check bouquet feasibility for each day and why checking every day is slow.
def minDays(bloomDay, m, k):
n = len(bloomDay)
if m * k > n:
return -1
min_day, max_day = min(bloomDay), max(bloomDay)
def canMake(day):
bouquets = flowers = 0
for bloom in bloomDay:
if bloom <= day:
flowers += 1
if flowers == k:
bouquets += 1
flowers = 0
else:
flowers = 0
return bouquets >= m
for day in range(min_day, max_day + 1):
if canMake(day):
return day
return -1
# Example usage
if __name__ == '__main__':
print(minDays([1,10,3,10,2], 3, 1)) # Output: 3
Line Notes
if m * k > n:Quickly eliminate impossible cases where total flowers needed exceed available flowers
min_day, max_day = min(bloomDay), max(bloomDay)Establish the search range for days
def canMake(day):Define feasibility function to check if bouquets can be made by this day
for bloom in bloomDay:Iterate over flowers to count consecutive bloomed flowers
if bloom <= day:Check if flower has bloomed by current day
if flowers == k:Once k consecutive flowers are found, increment bouquet count
for day in range(min_day, max_day + 1):Try every day in the range sequentially
if canMake(day):Return the first day that satisfies the bouquet requirement
import java.util.*;
public class Solution {
public static int minDays(int[] bloomDay, int m, int k) {
int n = bloomDay.length;
if (m * k > n) return -1;
int minDay = Integer.MAX_VALUE, maxDay = Integer.MIN_VALUE;
for (int day : bloomDay) {
minDay = Math.min(minDay, day);
maxDay = Math.max(maxDay, day);
}
for (int day = minDay; day <= maxDay; day++) {
if (canMake(bloomDay, m, k, day)) return day;
}
return -1;
}
private static boolean canMake(int[] bloomDay, int m, int k, int day) {
int bouquets = 0, flowers = 0;
for (int bloom : bloomDay) {
if (bloom <= day) {
flowers++;
if (flowers == k) {
bouquets++;
flowers = 0;
}
} else {
flowers = 0;
}
}
return bouquets >= m;
}
public static void main(String[] args) {
System.out.println(minDays(new int[]{1,10,3,10,2}, 3, 1)); // Output: 3
}
}
Line Notes
if (m * k > n) return -1;Eliminate impossible cases early
for (int day : bloomDay)Find min and max bloom days to define search range
for (int day = minDay; day <= maxDay; day++)Check each day sequentially
if (canMake(bloomDay, m, k, day)) return day;Return earliest feasible day
if (bloom <= day)Check if flower has bloomed by current day
if (flowers == k)Count bouquets when k consecutive flowers bloom
flowers = 0;Reset consecutive count when flower not bloomed
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool canMake(const vector<int>& bloomDay, int m, int k, int day) {
int bouquets = 0, flowers = 0;
for (int bloom : bloomDay) {
if (bloom <= day) {
flowers++;
if (flowers == k) {
bouquets++;
flowers = 0;
}
} else {
flowers = 0;
}
}
return bouquets >= m;
}
int minDays(vector<int>& bloomDay, int m, int k) {
int n = bloomDay.size();
if (m * k > n) return -1;
int minDay = *min_element(bloomDay.begin(), bloomDay.end());
int maxDay = *max_element(bloomDay.begin(), bloomDay.end());
for (int day = minDay; day <= maxDay; day++) {
if (canMake(bloomDay, m, k, day)) return day;
}
return -1;
}
int main() {
vector<int> bloomDay = {1,10,3,10,2};
cout << minDays(bloomDay, 3, 1) << endl; // Output: 3
return 0;
}
Line Notes
if (m * k > n) return -1;Check if making bouquets is impossible upfront
int minDay = *min_element(bloomDay.begin(), bloomDay.end());Find earliest bloom day
int maxDay = *max_element(bloomDay.begin(), bloomDay.end());Find latest bloom day
for (int day = minDay; day <= maxDay; day++)Try every day sequentially
if (canMake(bloomDay, m, k, day)) return day;Return first feasible day
if (bloom <= day)Check if flower has bloomed by current day
if (flowers == k)Count bouquets when k consecutive flowers bloom
flowers = 0;Reset consecutive count when flower not bloomed
function canMake(bloomDay, m, k, day) {
let bouquets = 0, flowers = 0;
for (const bloom of bloomDay) {
if (bloom <= day) {
flowers++;
if (flowers === k) {
bouquets++;
flowers = 0;
}
} else {
flowers = 0;
}
}
return bouquets >= m;
}
function minDays(bloomDay, m, k) {
const n = bloomDay.length;
if (m * k > n) return -1;
let minDay = Math.min(...bloomDay);
let maxDay = Math.max(...bloomDay);
for (let day = minDay; day <= maxDay; day++) {
if (canMake(bloomDay, m, k, day)) return day;
}
return -1;
}
// Example usage
console.log(minDays([1,10,3,10,2], 3, 1)); // Output: 3
Line Notes
if (m * k > n) return -1;Quickly rule out impossible cases
let minDay = Math.min(...bloomDay);Find earliest bloom day to start search
let maxDay = Math.max(...bloomDay);Find latest bloom day to end search
for (let day = minDay; day <= maxDay; day++)Check each day sequentially
if (canMake(bloomDay, m, k, day)) return day;Return earliest day that works
if (bloom <= day)Check if flower has bloomed by current day
if (flowers === k)Count bouquets when k consecutive flowers bloom
flowers = 0;Reset consecutive count when flower not bloomed
TimeO(n * D) where D is the range of days (maxDay - minDay)
SpaceO(1) additional space
For each day in the range, we scan the bloomDay array once to check feasibility, leading to O(n*D) time.
💡 If n=10^5 and day range is large (up to 10^9), this approach is too slow to finish in reasonable time.
Interview Verdict: TLE
This approach is too slow for large inputs because it checks every day sequentially. It is useful only to understand the problem and feasibility check.