[LeetCode] 1472. Design Browser History

김민우·2023년 3월 18일
0

알고리즘

목록 보기
158/189

- Problem

1472. Design Browser History

- 내 풀이 (Doubly Linked List)

class Node:
    def __init__(self, url: str):
        self.page: str = url
        self.next: Node = None
        self.prev: Node = None

class BrowserHistory:

    def __init__(self, homepage: str):
        self.root = Node(homepage)
        
    def visit(self, url: str) -> None:
        self.root.next = Node(url)
        self.root.next.prev = self.root
        self.root = self.root.next        

    def back(self, steps: int) -> str:
        for _ in range(steps):
            if not self.root.prev:
                break
            self.root = self.root.prev
        return self.root.page

    def forward(self, steps: int) -> str:
        for _ in range(steps):
            if not self.root.next:
                break
            self.root = self.root.next
        return self.root.page

- 결과

  • 시간 복잡도: O(N)
  • 공간 복잡도: O(N)
profile
Pay it forward.

0개의 댓글