Python | Two Stacks Solution | Easy to Understand
\nclass BrowserHistory:\n    \n    \'\'\'\n    We use two stacks here because anytime we\'ll need to go forward or backward to the \n    most recent page we wer
```\nclass BrowserHistory:\n    \n    \'\'\'\n    We use two stacks here because anytime we\'ll need to go forward or backward to the \n    most recent page we were at. A Stack helps us accomplish exactly that\n    \n    Eg: We are at "YouTube" -> We then visit "Facebook" -> We then visit "Google"\n    At this point our prevStack = ["YouTube", "Facebook"]\n    If we called back(1), we need to go to Facebook (which was the most recent before Google)\n        - We need to go back to Facebook (top of our prevStack rn)\n        - But we need to add our curPage to the forward stack because I called forward(1) after this operation, I would need to go back to Google\n        \n    You should be able to do the same analysis for forward(steps) \n    \n    This should help you see the intuition behind using 2 stacks and the general logic\n    \'\'\'\n\n    def __init__(self, homepage: str):\n        self.curPage = homepage # We keep track of the page we are currently on\n        self.prevStack = [] # We keep track of the pages if we were to go back \n        self.forwardStack = [] # We keep track of the pages if we were to go forward\n\n    def visit(self, url: str) -> None:\n        \'\'\'\n        Step 1: Since we\'re visiting a new page from curPage (our current page/url), if we go back, we need to go back to curPage first, hence add curPage to the top of prevStack\n        \n        Step 2: Reset forwardStack as mentioned in question\n        \n        Step 3: Now we need to make sure we update our current page correctly\n        \'\'\'\n        \n        \n        self.prevStack.append(self.curPage) # Step 1\n        self.forwardStack = [] # Step 2\n        self.curPage = url # Step 3\n        \n\n    def back(self, steps: int) -> str:\n        \n        \'\'\'\n        \n        Algorithm: \n            Step 0. Keep repeating until we still have to go back\n            Step 1. If we\'re going to go back from our curPage to page X, if we go forward from page X, we need to land at curPage, so add it to the forwardStack\n            Step 2. We need to go back now so update the curPage as needed and remove it from the prevStack\n            Step 3. We went back one more step at this point, so decrement possible by 1\n        \n        \'\'\'\n        \n        possible = min(steps, len(self.prevStack)) # In order to hamdle cases where steps > len(self.prevStack) ,we take the minimum of the two at all times\n        \n        while possible:  # Step 0\n            self.forwardStack.append(self.curPage) # Step 1\n            self.curPage = self.prevStack.pop() # Step 2\n            possible -= 1 # Step 3 \n        \n            \n        return self.curPage # We need to return the page we\'re at currently\n    \n\n    def forward(self, steps: int) -> str:\n        \n        \'\'\'\n        \n        Algorithm: \n            Step 0. Keep repeating until we still have to go forward\n            Step 1. If we\'re going to go forward from our curPage to page X, if we go backward from page X, we need to land at curPage, so add it to our prevStack\n            Step 2. We need to go forward now so update the curPage as needed and remove it from the forwardStack\n            Step 3. We went forward one more step at this point, so decrement possible by 1\n        \n        \'\'\'\n        \n        possible = min(steps, len(self.forwardStack)) # In order to hamdle cases where steps > len(self.forwardStack) ,we take the minimum of the two at all times\n        \n        while possible:  # Step 0\n            self.prevStack.append(self.curPage) # Step 1\n            self.curPage = self.forwardStack.pop() # Step 2\n            possible -= 1 # Step 3\n        \n        return self.curPage # We need to return the page we\'re at currently\n```\n        \n\nI\'ve put in comments in my code so I hope that helps with understanding the code :))\n\n
['Python']