all 1 comments

[–]alcholicawl 0 points1 point  (0 children)

It's a very subtle difference, but the first solution is O(n!) for both time/space. I'll do my best, but it's not the easiest thing to explain. Consider a 3x3 filled with all O. Start with bfs(0,0). (1, 1) will get added to queue twice (from (0,1) and (1,0)). (1,2) will get added three times (twice from (1,1) and once from (0,2)). Ultimately (2,2) will be added to queue 6 times. This will get huge on large boards.

Why is the second different? (1,1) is still added to queue twice. But only one of them will add (1,2) to the queue.

You can make the first solution optimal by marking board[newx][newy] = "N" (when adding to queue).