forked from Kyrylo-Ktl/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign Browser History.py
More file actions
36 lines (31 loc) · 886 Bytes
/
Design Browser History.py
File metadata and controls
36 lines (31 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class BrowserHistory:
"""
Memory:
creation - O(1)
operations - O(1)
Time:
creation - O(1)
visit - O(1)
back, forward - O(k)
"""
def __init__(self, homepage: str):
self.history = [homepage]
self.curr = 0
self.forw = 0
def visit(self, url: str) -> None:
self.curr += 1
self.forw = 0
if self.curr < len(self.history):
self.history[self.curr] = url
else:
self.history.append(url)
def back(self, steps: int) -> str:
for i in range(min(steps, self.curr)):
self.curr -= 1
self.forw += 1
return self.history[self.curr]
def forward(self, steps: int) -> str:
for i in range(min(steps, self.forw)):
self.curr += 1
self.forw -= 1
return self.history[self.curr]