Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Curious if anyone has found a way to solve this problem with the iterative BFS a...

J

Nov 15, 2022

Curious if anyone has found a way to solve this problem with the iterative BFS approach without using an extra matrix or map or set (for tracking visited cells) to reduce the space complexity to O(min(M, N)) .

0

0

Comments
Comments
M
Mike Xu3 years ago

You cannot solve it without using a visited set/matrix. If you override a cell's value in a grid, then the algorithm can tell if this cell has been visited before, but it cannot tell if this cell had the same value as the current value of interest or if it had a differe...

L
lejafilip a year ago

You can never solve the problem without "visited cells" because you can't destroy the input.

Em Eff
Em Eff7 months ago

In a matrix with lowercase letters, you could probably do it by marking a cell as visited by uppercasing the letter, and then iterate through the matrix and reset the characters back to lowercase before returning the result. Still would have the same time overall comple...

On this page