Untitled

 avatar
unknown
plain_text
2 years ago
742 B
6
Indexable
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;
}
Editor is loading...