Last active
March 14, 2022 09:15
-
-
Save doyedele1/e422a5b1cc2c3edfa5e465674b959cc0 to your computer and use it in GitHub Desktop.
Design Browser History
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Explanation: | |
- history: [leetcode, youtube, facebook, google] | |
- urlPosition: i | |
visit: pop out all urls after the urlPosition, append the url to visit and move the urlPosition | |
- history: [leetcode, youtube, facebook, google], steps = 2 | |
- urlPosition: i | |
back and forward --> you don't want to go out of bound because of the steps | |
TC - O(n) for all functions | |
SC - O(n) for all functions --> to store the browserHistory | |
''' | |
class BrowserHistory: | |
def __init__(self, homepage: str): | |
self.browserHistory = [homepage] | |
self.urlPosition = 0 | |
def visit(self, url: str) -> None: | |
while self.urlPosition < len(self.browserHistory) - 1: | |
self.browserHistory.pop() | |
self.browserHistory.append(url) | |
self.urlPosition += 1 | |
def back(self, steps: int) -> str: | |
while self.urlPosition > 0 and steps > 0: | |
self.urlPosition -= 1 | |
steps -= 1 | |
return self.browserHistory[self.urlPosition] | |
def forward(self, steps: int) -> str: | |
while self.urlPosition < len(self.browserHistory) - 1 and steps > 0: | |
self.urlPosition += 1 | |
steps -= 1 | |
return self.browserHistory[self.urlPosition] | |
# Your BrowserHistory object will be instantiated and called as such: | |
# obj = BrowserHistory(homepage) | |
# obj.visit(url) | |
# param_2 = obj.back(steps) | |
# param_3 = obj.forward(steps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment