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

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

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) —

推荐阅读:
描写菜地的作文  淡雅素然,是生活最美的格调  花儿不该这么早凋谢——安全在我心中作文650字  滑稽列传原文及翻译  游侠列传序原文及翻译  酷吏列传序原文及翻译  屈原列传原文及翻译  管晏列传原文及翻译  伯夷列传原文及翻译  外戚世家序原文及翻译 
评论列表
添加评论