684. Minimum Time To Eat All Grains
On a line are m grains and n chickens. The starting locations of the grains and chickens in two integer arrays of size n and m, respectively, are provided to you.
If they are in the same position, any hen can consume a grain. This process takes very little time. Moreover, a hen can consume more than one grain.
A hen can move one unit to the right or left in one second. The chickens are able to move independently and concurrently.
If the hens act optimally, return the minimal amount of time to consume all the grains.
Example 1:
Input : hens = [4, 1, 6, 9] , grains = [1, 5, 3, 11]
Output : 2
Explanation :
The hens could move as mentioned below :
0th index hen -> It can move to position 3 and eat the grain. Time taken will be 1sec.
1st index hen -> It can move to any position as the grain is present at same position. Time taken will be 0sec.
2nd index hen -> It can move to position 5 and eat the grain. Time taken will be 1sec.
3rd index hen -> it can move to position 11 and eat grain. Time taken will be 2 sec.
So total time taken to eat all grains is 2 sec. (There could be multiple ways in which 2 sec time can be acheived).
It can be proven that the hens cannot eat all grains before 2 seconds.
Example 2:
Input : hens = [3] , grains = [1, 2, 3, 4, 6]
Output : 7
Explanation :
The hens could move as mentioned below :
0th index hen -> It can move to position 2 -> position 1 -> position 4 -> position 6.
So total time taken will be 7.
Now Your Turn!
Pick the correct output for the given inputInput : hens = [1, 6] , grains = [1, 2, 3, 4, 6]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= hens.length, grains.length <= 2*104
- 0 <= hens[i], grains[j] <= 109