Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
742 B
2
Indexable
Never
int findMinimumNumberOfCandies(int[] candies, int hour) {
	int max = 0;

	for(int candie : candies) {
		max = Math.max(max, candie);
	}

	start = 0; 
	end = max;

	int result = Integer.Max_Value;


	while(start <= end) {

		int mid = start + (end - start)/2;

		boolean canFinish = canFinishCandies(candies, hour, mid);	

		if (canFinish) {
			result = Math.min(result, mid);
			end = mid - 1;
		} else {
			start = mid + 1;
		}
	}

	return result == Integer.Max_Value ? -1 : result;
}

boolean canFinishCandies(int[] candies, int hour, int candiesPerHour) {

	int neededHours = 0;

	for(int candie : candies) {
		neededHours += (int) Math.ceiling(candie/candiesPerHour);

		if (neededHours > hour) {
			return false;
		}
	}
	
	return true;
}