We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Each state is defined by 3 numbers (row1, row2, row3) and will have the value of either "win" or "lose".
The fundamental state is (0,0,0) with a value "win"
Now at any given state, you can have a number of moves which will transition to another state. For example, the state (2,2,1) has the following possible moves:
Since there is one move you can make from (2,0,0) that is a lose, then (2,0,0) is a win.
Many moves overlap, the moves from (10,10,10) have a massive overlap with the moves from (9,9,9), and each move would have to recursively calculate if it is a win or lose. This can be efficiently solved by memoizing the wins/loses for every state, so every state is only calculated once.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bitter Chocolate
You are viewing a single comment's thread. Return to all comments →
Here is the logic in a nutshell:
SPOILER ALERT
Each state is defined by 3 numbers (row1, row2, row3) and will have the value of either "win" or "lose".
The fundamental state is (0,0,0) with a value "win"
Now at any given state, you can have a number of moves which will transition to another state. For example, the state (2,2,1) has the following possible moves:
A state (row1, row2, row2) is a win if there is a possible transition to a lose state.
for example:
There is only 1 move from (1,0,0), and that move happens to be a win. Since all the moves from (1,0,0) are wins, then (1,0,0) is a lose.
Another example:
Since there is one move you can make from (2,0,0) that is a lose, then (2,0,0) is a win.
Many moves overlap, the moves from (10,10,10) have a massive overlap with the moves from (9,9,9), and each move would have to recursively calculate if it is a win or lose. This can be efficiently solved by memoizing the wins/loses for every state, so every state is only calculated once.