Your browser does not seem to support JavaScript. As a result, your viewing experience will be diminished, and you have been placed in read-only mode.

Please download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript).

Click here to see the full article post

The space complexity of Approach #2 is wrong. It should be O(N * m * n).

@Aeonaxx I have updated it. THanks.

how to deal with overflow in the recursion method?

For the first approach's time complexity, better say O(4^{N}) instead of redefining n (to what N already is).

Approach #2 is definitely wrong. We should do modulo operation on result. After modified, it'll still get TLE.

@Apolloliu I have updated the Approach #2. Its now AC. Thanks

Would an interviewer really care for approach 2 vs 3?

A remainder, the author uses int type, you must do the mode for every DFS return value, otherwise the total summation may overflow cause weird wrong result. You can use long type to avoid this kind of problem.

@tony407 So how would you do it with long, exactly?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.