Reputation: 341
Problem statement there are 'n' factories, situated at arr[i]. The entry is at '0' and the exit id at 'exit-pos' . The moving time form one place to another is the distance between the two.
Each factory takes 'processing' time (same for all) to process. You have to start from '0' , exit from 'exit-pos' .
Also you have to produce 'n' units, each factory can produce 1 unit only. Along the path you need to visit each factory and give the order to produce. Then it will take ' processing' time to produce it. you can wait that time for the unit to produce and move ahead. Or you can visit other factories to give them order. latter you need to visit the factory again to pick the order.
Time taken to travel is the distance between the two places.
Question: You need to tell the minimum time taken to produce 'n' units and collect them also. Your journey starts at '0' and ends at a 'end-pos'.
Input:
number of units required to produce = number of factories = n
the location of factories in a array = arr
the exit position = ext-pos
the processing time at each unit (it is same for each factory) = processing-time.
my approach I had this question during a test. Test is submitted but unfortunately no answers were given. During the test, I tried to used a recursive approach. It considered the 'items-collected', 'time', 'factory - position'. It failed. It got into a time loop.
Any suggestion/ question is there regarding question's clarity feel free to comment. Thanks
update: 0 < arrr[i] (factory time) < exiPos
Upvotes: 2
Views: 59
Reputation: 1349
I've uploaded the solution to a GitHub repo.
You can see a live demo here.
Here are the contents of the README with a screenshot:
Since 0 < arr[i] < exit-pos
, we always start at the first factory and finish at the last. It's trivial to add the remaining time.
The solution includes three different algorithms, which can be selected via a drop-down:
Explores all states and finds the minimum time to a final state:
The A* algorithm is basically the same as Dijkstra's algorithm, but uses a heuristic to favor more promising nodes.
The heuristic used calculates the minimum travel distance without waiting. It computes the time to the leftmost unfinished factory and from there to the exit.
The improvement is significant (even for small inputs).
This version uses the same search as the previous, but restricts new states being explored:
It follows the strategy to finish everything left of you once you turn from going right to going left. And waiting is only allowed at leftmost unfinished factory.
Why does this work (references above list)?
These are rough timings for this input:
n = 14
arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43]
processingTime = 15
Algorithm | Time |
---|---|
Dijkstra | ~ 3000 ms |
A* | ~ 100 ms |
Strategy | ~ 10 ms |
First I chose to visualize the solution and solve it by using Dijkstra's algorithm.
After inspecting the paths for various inputs, I noticed the pattern described in the "Strategy" solution: Going right, turning around and then completing this chunk of factories.
One could take the strategy and chunk the factories array.
Each chunk [a1, a2, ..., an]
takes travelTime + waitTime = [3 * (an-a1)] + [min(0, processingTime - 2 * (an-a1))]
time to finish.
And travelling between chunks [a1, ..., an]
and [b1, ..., bm]
takes b1 - an
time.
Now one has to determine the chunks, which minimize the total time.
I had too much fun working this out.
And I can only recommend visualizing things.
One note to me: Think before you code. Just take a piece of paper and draw what you would do without a computer.
Upvotes: 1