#🔒 (BackTracking) Can someone explain this to me?

5 messages · Page 1 of 1 (latest)

magic vale
#

I saw this solution that i couldn't come up with on LeetCode, it's a backtracking solution using recursion, I can understand how we got the first item in the results but not the rest, My understanding is that once 3 = 3 =3, the array is appended and the recursion goes back to the last (, in which it starts another closedN<openN in this case it's 0<3, but the stack will have ((... How did they even get up with this at first place damn.

harsh thunderBOT
#

@magic vale

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

robust rain
#

This basicly produces all the combinations of 3 (n=3, yes?) brackets with matching closes.

So the deal is that it's kind of exploring the space of ( and ) combinations. It only goes down particular branches.

If you've got the right number of opens and closes, return from this leaf call. Do not descend any deeper.

If you lack sufficient opens, append an open and call yourself to explore that branch of the tree. Then remove the open you appended.

If you lack sufficient closes, append a close and explore that branch pf the tree. Then remove the close.

Do not explore anything else.

So the stack is the path into the tree from the root ("") to some path composed of ( and ) characters.

Think of it a bit like solve the tower-of-hanoi with the "move the top disc to the side, move the bottom disc, move the top discs onto the bottom disc" which is a similar recursive framing of the problem.

harsh thunderBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.