-
Notifications
You must be signed in to change notification settings - Fork 1
/
1472_design_browser_hostory.cpp
142 lines (124 loc) · 3.45 KB
/
1472_design_browser_hostory.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include<bits/stdc++.h>
using namespace std;
/*
1472. Design Browser History
Medium
2.2K
143
Companies
You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps.
Implement the BrowserHistory class:
BrowserHistory(string homepage) Initializes the object with the homepage of the browser.
void visit(string url) Visits url from the current page. It clears up all the forward history.
string back(int steps) Move steps back in history. If you can only return x steps in the history and steps > x, you will return only x steps. Return the current url after moving back in history at most steps.
string forward(int steps) Move steps forward in history. If you can only forward x steps in the history and steps > x, you will forward only x steps. Return the current url after forwarding in history at most steps.
*/
class BrowserHistory {
stack<string>bck,fwd;
string curr;
public:
BrowserHistory(string homepage) {
curr=homepage;
}
void visit(string url) {
bck.push(curr);
curr=url;
while(!fwd.empty())
fwd.pop();
}
string back(int steps) {
string temp=curr;
fwd.push(curr);
while(!bck.empty() && steps>0)
{
temp=bck.top();
bck.pop();
steps--;
if(!bck.empty() && steps>0)
fwd.push(temp);
else
break;
}
if(temp==curr)
fwd.pop();
curr=temp;
return curr;
}
string forward(int steps) {
string temp=curr;
bck.push(curr);
while(!fwd.empty() && steps>0)
{
temp=fwd.top();
fwd.pop();
steps--;
if(!fwd.empty() && steps>0)
{
bck.push(temp);
}
else
break;
}
if(temp==curr)
bck.pop();
curr=temp;
return curr;
}
};
/**
* Your BrowserHistory object will be instantiated and called as such:
* BrowserHistory* obj = new BrowserHistory(homepage);
* obj->visit(url);
* string param_2 = obj->back(steps);
* string param_3 = obj->forward(steps);
*/
// OR can also go for doubly linked list approach -> more efficient than STACK
class Node{
public:
string val;
Node* next;
Node* prev;
Node(string url){
this->val = url;
next = NULL;
prev = NULL;
}
};
class BrowserHistory {
public:
Node* head;
Node* curr;
BrowserHistory(string homepage) {
head = new Node(homepage);
curr = head;
}
void visit(string url) {
Node* temp = new Node(url);
curr->next = temp;
temp->prev = curr;
curr = temp;
}
string back(int steps) {
while(steps--){
if(curr->prev == NULL)
return curr->val;
curr = curr->prev;
}
return curr->val;
}
string forward(int steps) {
while(steps--){
if(curr->next == NULL)
return curr->val;
curr = curr->next;
}
return curr->val;
}
};
/**
* Your BrowserHistory object will be instantiated and called as such:
* BrowserHistory* obj = new BrowserHistory(homepage);
* obj->visit(url);
* string param_2 = obj->back(steps);
* string param_3 = obj->forward(steps);
*/