-
Notifications
You must be signed in to change notification settings - Fork 0
/
BrowserHistory.java
122 lines (103 loc) · 2.99 KB
/
BrowserHistory.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package solutions;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
// [Problem] https://leetcode.com/problems/design-browser-history
// List and current page index
class BrowserHistoryList {
List<String> history;
int currentPage;
public BrowserHistoryList(String homepage) {
super();
this.history = new ArrayList<>();
this.history.add(homepage);
this.currentPage = 0;
}
// O(1) time, O(1) space
public void visit(String url) {
history.subList(currentPage + 1, history.size()).clear();
history.add(url);
currentPage++;
}
// O(1) time, O(1) space
public String back(int steps) {
currentPage = Math.max(currentPage - steps, 0);
return history.get(currentPage);
}
// O(1) time, O(1) space
public String forward(int steps) {
currentPage = Math.min(currentPage + steps, history.size() - 1);
return history.get(currentPage);
}
}
// Two stacks
class BrowserHistoryStack {
Stack<String> backwardHistory;
Stack<String> forwardHistory;
public BrowserHistoryStack(String homepage) {
super();
this.forwardHistory = new Stack<>();
this.backwardHistory = new Stack<>();
this.backwardHistory.add(homepage);
}
// O(1) time, O(1) space
public void visit(String url) {
backwardHistory.add(url);
forwardHistory = new Stack<>();
}
// O(n) time, O(1) space
public String back(int steps) {
while (steps-- > 0 && backwardHistory.size() > 1) {
forwardHistory.add(backwardHistory.pop());
}
return backwardHistory.peek();
}
// O(n) time, O(1) space
public String forward(int steps) {
while (steps-- > 0 && !forwardHistory.isEmpty()) {
backwardHistory.add(forwardHistory.pop());
}
return backwardHistory.peek();
}
}
// Doubly-Linked List
class BrowserHistoryDoublyLinkedList {
class Page {
String url;
Page next;
Page prev;
public Page(String url) {
this.url = url;
next = null;
prev = null;
}
}
Page homepage;
Page currentPage;
public BrowserHistoryDoublyLinkedList(String homepage) {
super();
this.homepage = new Page(homepage);
currentPage = this.homepage;
}
// O(1) time, O(1) space
public void visit(String url) {
Page newPage = new Page(url);
currentPage.next = newPage;
newPage.prev = currentPage;
currentPage = newPage;
}
// O(n) time, O(1) space
public String back(int steps) {
while (steps-- > 0 && currentPage.prev != null) {
currentPage = currentPage.prev;
}
return currentPage.url;
}
// O(n) time, O(1) space
public String forward(int steps) {
while (steps-- > 0 && currentPage.next != null) {
currentPage = currentPage.next;
}
return currentPage.url;
}
}