This problem is closely related to Problem 2189, Order of things - 1. For the details, see the description for that problem. Basically, we have to find the order in which to execute tasks of which the results and prerequisites depend on each other.
Again, the dependencies of the tasks on each other is expressed in a matrix, where each row and column corresponds to a specific task. Each row expresses on which result that task depends. A 1 indicates that the calculation on that row depends on the one mentioned at the top of that column.
Return the new row/column order as a numeric row-vector, referring to the rows/columns of the input matrix. Of an empty array when no solution is possible. Or a matrix of rows containing the orders, where each row is a different solution, in case multiple solutions exist.
A B C D E A 0 1 0 0 0 B 0 0 0 0 0 C 1 0 0 1 0 D 1 0 1 0 0 E 1 1 1 1 0
The above problem can not be solved, since C depends on D, which in its place depends on C. The returned value would be [] .
A B C D E A 0 1 0 0 0 B 0 0 0 0 0 C 1 0 0 0 0 D 1 0 0 0 0 E 1 1 1 1 0
The returned matrix should be
[ 2 1 3 4 5 2 1 4 3 5 ]
Good luck!
1008 Solvers
172 Solvers
Longest run of consecutive numbers
294 Solvers
287 Solvers
479 Solvers
Solution 406514
Since Cody, I dislike for-loops, but your solution is way simpler than my own algorithm. Mission accomplished...
Nevertheless, I would hope for a non-looped piece of code. Let's see in phase 3.