Skip to content
Advertisement

Time Limit Exceed for BFS algorithm

It is leetcode #200, number of islands. My code is

JavaScript

It works fine. But when I change the bfs function into

JavaScript

It gives me the Time Limit Exceeded error, what could the reasons be? I think these two codes are nearly identical?

Advertisement

Answer

You are visiting the same state many times:

Change to the following:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement