Skip to main content
Commonmark migration
Source Link

##Bug##

Bug

Your program failed with this input:

        matrix[0] = new int[]{0, 0, 1, 1, 1};
        matrix[1] = new int[]{0, 1, 1, 1, 1};
        matrix[2] = new int[]{0, 1, 0, 0, 1};
        matrix[3] = new int[]{0, 0, 0, 0, 1};
        matrix[4] = new int[]{0, 0, 0, 0, 1};

You can see the result here at ideone.

The reason for the failure is that your backtracking (following the 2's) is susceptible to running itself into a dead end. I'm not convinced that you can ever get your current method to work for all test cases.

##Is it really O(1) space?##

Is it really O(1) space?

Since you are storing integers into your matrix, I'm not sure I would call that an O(1) space solution. If you stored a direction into each cell as you passed through it, you could easily implement a DFS that way. So I believe your solution is really O(N) space.

##Bug##

Your program failed with this input:

        matrix[0] = new int[]{0, 0, 1, 1, 1};
        matrix[1] = new int[]{0, 1, 1, 1, 1};
        matrix[2] = new int[]{0, 1, 0, 0, 1};
        matrix[3] = new int[]{0, 0, 0, 0, 1};
        matrix[4] = new int[]{0, 0, 0, 0, 1};

You can see the result here at ideone.

The reason for the failure is that your backtracking (following the 2's) is susceptible to running itself into a dead end. I'm not convinced that you can ever get your current method to work for all test cases.

##Is it really O(1) space?##

Since you are storing integers into your matrix, I'm not sure I would call that an O(1) space solution. If you stored a direction into each cell as you passed through it, you could easily implement a DFS that way. So I believe your solution is really O(N) space.

Bug

Your program failed with this input:

        matrix[0] = new int[]{0, 0, 1, 1, 1};
        matrix[1] = new int[]{0, 1, 1, 1, 1};
        matrix[2] = new int[]{0, 1, 0, 0, 1};
        matrix[3] = new int[]{0, 0, 0, 0, 1};
        matrix[4] = new int[]{0, 0, 0, 0, 1};

You can see the result here at ideone.

The reason for the failure is that your backtracking (following the 2's) is susceptible to running itself into a dead end. I'm not convinced that you can ever get your current method to work for all test cases.

Is it really O(1) space?

Since you are storing integers into your matrix, I'm not sure I would call that an O(1) space solution. If you stored a direction into each cell as you passed through it, you could easily implement a DFS that way. So I believe your solution is really O(N) space.

Source Link
JS1
  • 28.9k
  • 3
  • 42
  • 83

##Bug##

Your program failed with this input:

        matrix[0] = new int[]{0, 0, 1, 1, 1};
        matrix[1] = new int[]{0, 1, 1, 1, 1};
        matrix[2] = new int[]{0, 1, 0, 0, 1};
        matrix[3] = new int[]{0, 0, 0, 0, 1};
        matrix[4] = new int[]{0, 0, 0, 0, 1};

You can see the result here at ideone.

The reason for the failure is that your backtracking (following the 2's) is susceptible to running itself into a dead end. I'm not convinced that you can ever get your current method to work for all test cases.

##Is it really O(1) space?##

Since you are storing integers into your matrix, I'm not sure I would call that an O(1) space solution. If you stored a direction into each cell as you passed through it, you could easily implement a DFS that way. So I believe your solution is really O(N) space.