LeetCode 2686: Immediate Food Delivery III
Problem Description
Explanation:
Given a list of food delivery times and the time it takes for the courier to reach the recipient, we need to find the time that the last order can be delivered immediately.
To solve this problem, we can use binary search to find the minimum possible time that can satisfy all delivery requests. We will maintain a time window and check if it is possible to deliver all orders within that time. If it is possible, we update the answer and narrow down the time window. If it is not possible, we widen the time window. Solution:
Solutions
class Solution {
public int minDeliveryTime(int[] deliveryTimes, int time) {
int left = 0, right = 1000000;
int ans = 0;
while (left <= right) {
int mid = left + (right - left) / 2;
int cnt = 0;
for (int i = 0; i < deliveryTimes.length; i++) {
cnt += Math.max(0, mid - deliveryTimes[i]);
}
if (cnt <= time) {
ans = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return ans;
}
}
Loading editor...