【leetcode】1482. Minimum Number of Days to Make m Bouquets

提交代码

class Solution {
    public int minDays(int[] bloomDay, int m, int k) {
    	
    	int[] bd=bloomDay.clone();
    	Arrays.sort(bloomDay);
    	
    	int i=0,j=bloomDay.length-1,mid=(i+j)/2;
    	while(i<j) {
    		if(i==j-1) {
    			if(hasBouquet(bd, bloomDay[i], m,k))
    				return bloomDay[i];
    			if(hasBouquet(bd, bloomDay[j], m,k))
    				return bloomDay[j];
    			return -1;
    		}
    		if(hasBouquet(bd, bloomDay[mid], m,k))
    			j=mid;
    		else
    			i=mid;
    		mid=(i+j)/2;
    	}
    	return bloomDay[mid];
    	
  
    }
    
    private boolean hasBouquet(int[] bd, int day, int m, int k) {
    	int p=0,curK=0;
    	while(p<bd.length) {
    		if(day>=bd[p]) {
    			curK++;
    			if(curK==k) {
    				m--;
    				curK=0;
    				if(m==0) 
    					return true;
    			}
    		}else {
    			curK=0;
    		}
    		p++;
    	}
    	return false;
    }

    
    
}

运行结果

【leetcode】1482. Minimum Number of Days to Make m Bouquets_第1张图片

你可能感兴趣的:(leetcode)