🟩O(1) time beats 92.46% Nice and clear 🟩🟩🟩
Intuition\n Describe your first thoughts on how to solve this problem. \n\nThis problem simulates the behavior of a web browser history. We need to implement fu
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n![image.png](https://assets.leetcode.com/users/images/fc2bdbd6-9806-4da1-86e1-1784653473e6_1706545895.6631844.png)\nThis problem simulates the behavior of a web browser history. We need to implement functionalities to visit a new URL, go back a certain number of steps, and go forward a certain number of steps.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can maintain a stack to keep track of visited URLs. Whenever we visit a new URL, we push it onto the stack. When we go back, we pop URLs from the stack according to the given steps. Similarly, when we go forward, we move forward in the stack according to the given steps.\n# Complexity\n- Time complexity:O(1)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass BrowserHistory:\n\n    def __init__(self, homepage: str):\n        self.stack=[homepage]\n        self.cur=0\n\n    def visit(self, url: str) -> None:\n       \n        self.stack=self.stack[:self.cur+1]\n        self.cur+=1\n        self.stack.append(url)\n        \n        \n\n    def back(self, steps: int) -> str:\n       \n        if steps <= self.cur:\n            self.cur-=steps\n        else:\n            self.cur=0\n   \n        return self.stack[self.cur]\n\n    def forward(self, steps: int) -> str:\n        if self.cur + steps < len(self.stack):\n            self.cur +=steps\n        else:\n            self.cur = len(self.stack)-1\n        return self.stack[self.cur]\n        \n\n\n# Your BrowserHistory object will be instantiated and called as such:\n# obj = BrowserHistory(homepage)\n# obj.visit(url)\n# param_2 = obj.back(steps)\n# param_3 = obj.forward(steps)\n```
['Python3']