How to Design a Browser History using Double-ended Queue (deque)

  • 时间:2020-09-09 13:08:38
  • 分类:网络文摘
  • 阅读:100 次

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.

Example:

Input:

1
2
3
4
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
Output:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"]
[["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
Output:
[null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]

Explanation:

1
2
3
4
5
6
7
8
9
10
11
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
browserHistory.visit("google.com");       // You are in "leetcode.com". Visit "google.com"
browserHistory.visit("facebook.com");     // You are in "google.com". Visit "facebook.com"
browserHistory.visit("youtube.com");      // You are in "facebook.com". Visit "youtube.com"
browserHistory.back(1);                   // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
browserHistory.back(1);                   // You are in "facebook.com", move back to "google.com" return "google.com"
browserHistory.forward(1);                // You are in "google.com", move forward to "facebook.com" return "facebook.com"
browserHistory.visit("linkedin.com");     // You are in "facebook.com". Visit "linkedin.com"
browserHistory.forward(2);                // You are in "linkedin.com", you cannot move forward any steps.
browserHistory.back(2);                   // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
browserHistory.back(7);                   // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com" 
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
browserHistory.visit("google.com");       // You are in "leetcode.com". Visit "google.com"
browserHistory.visit("facebook.com");     // You are in "google.com". Visit "facebook.com"
browserHistory.visit("youtube.com");      // You are in "facebook.com". Visit "youtube.com"
browserHistory.back(1);                   // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
browserHistory.back(1);                   // You are in "facebook.com", move back to "google.com" return "google.com"
browserHistory.forward(1);                // You are in "google.com", move forward to "facebook.com" return "facebook.com"
browserHistory.visit("linkedin.com");     // You are in "facebook.com". Visit "linkedin.com"
browserHistory.forward(2);                // You are in "linkedin.com", you cannot move forward any steps.
browserHistory.back(2);                   // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
browserHistory.back(7);                   // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com" 

Constraints:
1 <= homepage.length <= 20
1 <= url.length <= 20
1 <= steps <= 100
homepage and url consist of ‘.’ or lower case English letters.
At most 5000 calls will be made to visit, back, and forward.

Hints:
Use two stack one for back history and one for forward history and simulate the functions.
Can you do faster by using different data structure?

Using deque in C++

The deque is a double-ended Queue in C++ meaning that you can push and pop in both directions.

Thus, we also need a cursor to store the current position in the queue, and update accordingly.

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
class BrowserHistory {
public:
    BrowserHistory(string homepage) {
        history.push_back(homepage);
        cursor = 0;
    }
    
    void visit(string url) {
        while (history.size() - 1 > cursor) {
            history.pop_back();
        }
        history.push_back(url);
        cursor ++;
    }
    
    string back(int steps) {
        if (steps > cursor) {
            cursor = 0;
            return history[0];
        }
        cursor -= steps;
        return history[cursor];
    }
    
    string forward(int steps) {
        if (steps + cursor >= history.size()) {
            cursor = history.size() - 1;
            return history[cursor];
        }
        cursor += steps;
        return history[cursor];
    }
private:
    int cursor;
    deque<string> history;
};
 
/**
 * 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);
 */
class BrowserHistory {
public:
    BrowserHistory(string homepage) {
        history.push_back(homepage);
        cursor = 0;
    }
    
    void visit(string url) {
        while (history.size() - 1 > cursor) {
            history.pop_back();
        }
        history.push_back(url);
        cursor ++;
    }
    
    string back(int steps) {
        if (steps > cursor) {
            cursor = 0;
            return history[0];
        }
        cursor -= steps;
        return history[cursor];
    }
    
    string forward(int steps) {
        if (steps + cursor >= history.size()) {
            cursor = history.size() - 1;
            return history[cursor];
        }
        cursor += steps;
        return history[cursor];
    }
private:
    int cursor;
    deque<string> history;
};

/**
 * 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);
 */

As the visit rewrites the forward history, we need to pop back all the URLs from the current cursor+1 to the end of the queue before we push the new URL to the queue (rewrite the forward history). The time complexity is O(N) where N is the maximum number of the URLs in history.

The back and forward operations are trivial O(1) time complexity as we only need to check the boundaries and update the cursor accordingly.

Oh, just realised that in this particular problem, you don’t need deque, you can use just use a stack (as we are only push and pop from one-end only).

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
Celebrate WordPress’ 13th Birthday With These Interesting Facts  5 Ways to Use Your Smartphone to Build a Better Blog  The Most Expensive Domain Sales Ever  15 Ways Of How Not To Kill Your Leadership Authority  Study Shows Strong Growth of Tech Inventions to Fight Climate Ch  Interested in Bitcoins? Here are 10 Blogs You Need to Check Out  Your Blog’s Been Hacked! Here’s What You Need to Do  #DiningForBrussels: How Belgium Is Fighting Terrorism With A For  Many WordPress Site Hackings Caused To These Plugins  GoDaddy Company Now Hosts New Enterprise-Level Plans 
评论列表
添加评论