I think you can improve this by looking at the problem as a directed graph of pairs of positions.
For this example I will use the line with values -9, -6, -1, 3, and 5.
Because it's too hard to draw a graph with just text, I'm going to represent the pairs as a table. We can think of the cells as representing the state where all containers between those two positions have been collected. Each cell has two values, one representing the cost to go left, the other representing the cost to go right (to the next container).
The values can simply be calculated as the distance between the two points multiplied by the number of barrels outside of those two points + 1. Cells where both numbers have the same sign represent cases when all barrels of the opposite sign have been collected. These are calculated using only the number of barrels in the direction away from zero.
In the table values of X mean that you can't go in that direction (all the barrels in that direction have been taken). The rows represent the current position of the collector and the columns represent the location of the next opposite barrel.
+------+------+------+------+------+
| -9 | -6 | -1 | 3 | 5 |
+---+------+------+------+------+------+
|-9 | | | | X, 24| X, 14|
+---+------+------+------+------+------+
|-6 | 3, X | | | 9, 27| 6, 22|
+---+------+------+------+------+------+
|-1 | |10, X | |20, 8 |15, 18|
+---+------+------+------+------+------+
| 3 |24, 4 |27, 6 |16, 8 | | X, 2 |
+---+------+------+------+------+------+
| 5 |14, X |22, X |18, X | | |
+---+------+------+------+------+------+
The rules for moving between squares can be somewhat confusing.
For negative numbered columns, the rules are as follows:
- Going right moves down one cell.
- Going left moves down one cell and then mirrors across the diagonal.
- If the right value is X, going left moves up to the diagonal (where the column and row are equal) and left by one.
For positive numbered columns, the rules are as follows:
- Going left moves up one cell.
- Going right moves up one cell and then mirrors across the diagonal.
- If the left value is X, going right moves down to the diagonal and right by one.
Now we can run Dijkstra's algorithm to calculate the best path, using these movement rules to traverse the graph. Our starting positions are (-1, 3) and (3, -1) with initial costs of 5 and 15, respectively. Once we've calculated values for the two end positions (left of (-9, -9) and right of (5, 5)) the smaller of the two is our answer.
The running time for each of the steps are:
- O(N log N) for initially sorting the input values along the line
- O(N^2) for calculating the table/graph
- O(N^2 log N) for running Dijkstra's on the graph (Note: there are at most two edges for any given vertex).
The first two steps are dominated by the last, so your overall runtime is O(N^2 log N) which should be a good enough runtime for the challenge.
Greedy algorithms do not find optimal solutions for any nontrivial optimization problem. That is the reason why optimization is a whole field of scientific research and there are tons of different optimization algorithms for different categories of problems.
Moreover, "greedy algorithms" is only a category of optimization algorithms, for a given problem, there can be lots of different greedy algorithms, so it does not make much sense to ask "if applying greedy algorithm on a problem will yield optimal solution". What makes sense could be to ask "if applying a specific greedy algorithm" will always yield to an optimal solution, or to ask if for a given problem an "optimal" greedy algorithm exists (or not). Typically, there is no "optimal" greedy algorithm when a given optimization problem has local maxima which are not the global maximum, because a greedy algorithm will get stuck when it reaches such a local maximum.
Best Answer
The NP-Hard domain of problems means that, as far as current mathematical knowledge goes, the problem can only be solved by trying every permutation and choosing the correct answer.
If you can solve the problem more efficiently than the brute force method, you will win a Noble Prize in mathematics as a bonus. The best mathematicians have been working on a general answer to this problem for decades.
Perhaps as you are wanting to create a test data-set for your NP-Hard problem solver, your approach may be to design the test data backwards - rather than solve the NP-Hard problem, create an NP-Hard problem with a known answer - I don't even know if that is a NP-Hard problem on it's own.