1482. Minimum Number of Days to Make m Bouquets

Link

Solution

    public int minDays(int[] bloomDay, int m, int k) {
        int left = -1;
        int right = 0;
        for(int spendDay : bloomDay){
            left = Math.min(left, spendDay);
            right = Math.max(right, spendDay);
        }
        
        if(!isfeasible(bloomDay, right, m, k)) return -1;
        
        while(left < right){
            int mid = left + (right-left)/2;
            if(isfeasible(bloomDay, mid, m, k)){
                right = mid;
            }
            else{
                left = mid+1;
            }
        }
        return left;
    }
    
    public boolean isfeasible(int[] bloomDay, int curDay, int m, int k){
        int total = 0;
        int count = 0;
        for(int i = 0; i < bloomDay.length; i++){
            if(bloomDay[i] <= curDay){
                total++;
                if(total == k){
                    count++;
                    total = 0;
                    if(count == m) return true;
                }
                
            }else{
                total = 0;
            }
        }
        return false;
    }

Last updated

Was this helpful?